python爬虫重新连接的问题
求助大佬,我利用爬虫爬取网上的数据,但是当网络断开重新连接的时候爬虫的请求就会一直失败,无法重新爬去数据,只能重新启动程序,这样太麻烦了。
def getcatalog(self,url,data,header,ip):
n = 0
while True:
try:
HTML = requests.post(url,data,header,proxies = ip)
n = 0
return HTML.text
except Exception:
HTML.close()
ip = choice(global_var.ip_list).split(':', 1) #更换ip
global_var.IP = {'http': ip[1]}
n += 1
if n >= 5:
print('请求失败,1分钟后重新连接')
n = 0
time.sleep(60)
else:
print('请求异常,以更换代理IP,5秒后重新发起请求')
time.sleep(5)
我断开网络后重新连接就无法重新爬去数据,只能重新启动程序