我正在运行我的网络抓取脚本,它收集了数千个数据。问题是当出现错误时它会一直停止。我想要它只是记录错误并继续下一个URL。以下是我目前对异常
的看法uClient3 = ''
while uClient3 == '':
try:
uClient3 = requests.get(fsgsubcard2ref)
print("Proceding to the next level in...")
except:
print("Connection refused by the server..")
print("Let me sleep for 7 seconds")
print("ZZzzzz...")
time.sleep(8)
print("Was a nice sleep, now let me continue...")
continue
如何防止错误停止脚本并记录?
答案 0 :(得分:-1)
在uClient3 = requests.get(fsgsubcard2ref)
之后,uClient3
将成为商店Response
对象,您的条件while uClient3 == ''
将返回False
。我身边最好的方法就是使用for循环:
for i in fsgsubcard2ref: # fsgsubcard2ref should be a list() with all url, or you can can use variable with another name
try:
response = requests.get(i)
# processing
except:
print('Error')