[英]Python : correct way to formulate try-except
I'm scraping Tripadvisor with Scrapy ( https://www.tripadvisor.com/Hotel_Review-g189541-d15051151-Reviews-CitizenM_Copenhagen_Radhuspladsen-Copenhagen_Zealand.html ).
我抓取的項目之一是景點數量和半徑以及餐廳的數量和半徑。 此信息並不總是存在( https://www.tripadvisor.com/Hotel_Review-g189541-d292667-Reviews-Strandmotellet_Greve-Copenhagen_Zealand.html )。 如果它不存在,我會收到以下錯誤消息:“IndexError: list index out of range”( https://pastebin.com/pphM8FSM )
我試圖編寫一個嘗試錯誤的構造,但沒有成功:
try:
nearby_restaurants0_attractions1_distance = response.css("._1aFljvmJ::text").extract()
except IndexError:
nearby_restaurants0_attractions1_distance = [None,None]
items["hotel_nearby_restaurants_distance"] = nearby_restaurants0_attractions1_distance[1]
items["hotel_nearby_attractions_distance"] = nearby_restaurants0_attractions1_distance[2]
非常感謝你的幫助!
列表索引是從零開始的,而不是從一開始的。 如果您期望一個包含兩項的列表,則需要修改最后兩行以使用[0]
和[1]
而不是[1]
和[2]
:
items["hotel_nearby_restaurants_distance"] = nearby_restaurants0_attractions1_distance[0]
items["hotel_nearby_attractions_distance"] = nearby_restaurants0_attractions1_distance[1]
我也不確定 IndexError 是否來自數據丟失時。 即使數據存在,它也可能剛剛遇到這個錯誤。 如果數據丟失,您可能需要捕獲不同的異常。
有興趣的人回答:
Scrapy 在 near_restaurants0_attractions1_distance 中搜索項目,但如果找不到,則返回 None。 所以那個階段沒有IndexError。
當項目僅獲取列表的一部分時,稍后會發生 IndexError - 當 Scrapy 返回 None-Object 時顯然不存在。 [pastebin 還在 IndexError 上方的一行中顯示問題出在項目上]
nearby_restaurants0_attractions1_distance = response.css("._1aFljvmJ::text").extract()
try:
items["hotel_nearby_restaurants_distance"] = nearby_restaurants0_attractions1_distance[1]
except IndexError:
items["hotel_nearby_restaurants_distance"] = None
try:
items["hotel_nearby_attractions_distance"] = nearby_restaurants0_attractions1_distance[2]
except:
items["hotel_nearby_attractions_distance"] = None
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.