有一个爬虫,需要循环连续请求5个以上的url,
可能在某一次的循环中,出现某一个url请求报错:
Cannot connect to proxy.', RemoteDisconnected('Remote end closed connection without response')或者
http.client.RemoteDisconnected: Remote end closed connection without response之类的报错.
如果在每一个url的请求里加上try的话,感觉太复杂了,
目前只会关掉py文件重新运行,等遇到下次报错,再关掉重开,
有没有什么思路,让脚本遇到此类问题时,自动重试。
我建议还是try,然后except:里面写time.sleep(60),再次调用爬虫函数
##不用每一个url 都加try 吧,就是主循环加就行了,不管多少url 不都一样吗。给你个样例你改下
##成功就下一个,失败最多重试3次
i=0
while urllist:
url=urllist[0]
try:
res=request.get(url)
urllist.remove(urllist[0])
i=0
except:
if i>=3:
print(url,"重试3次失败,跳到下一个")
urllist.remove(urllist[0])
else:
i=i+1