[英]Handling a url which fails to open, error handling using urllib
我想要一些關於如何處理無法打開的url的幫助,當前整個程序在打開url時無法中斷(tree = ET.parse(opener.open(input_url)))...
如果我的第一個函數調用(motgift)上的url打開失敗,我希望它等待10秒然后再次嘗試打開url,如果它再次失敗,我希望我的腳本繼續下一個函數調用(觀察者) )。
def spider_xml(input_url, extract_function, input_xpath, pipeline, object_table, object_model):
opener = urllib.request.build_opener()
tree = ET.parse(opener.open(input_url))
print(object_table)
for element in tree.xpath(input_xpath):
pipeline.process_item(extract_function(element), object_model)
motgift = spider_xml(motgift_url, extract_xml_item, motgift_xpath, motgift_pipeline, motgift_table, motgift_model)
observer = spider_xml(observer_url, extract_xml_item, observer_xpath, observer_pipeline, observer_table, observer_model)
會非常高興並欣賞如何實現這一目標的一個例子。
try:
resp = opener.open(input_url)
except Exception:
time.sleep(10)
try:
resp = opener.open(input_url)
except Exception:
pass
你在找這個嗎?
Try Trycept塊是否有效?
error = 0
while error < 2:
try:
motgift = spider_xml(motgift_url, extract_xml_item, motgift_xpath, motgift_pipeline, motgift_table, motgift_model
break
except:
error += 1
sleep(10)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.