如何使用错误消息继续报废

时间:2017-09-21 06:02:58

标签: python web-scraping

我正在运行我的网络抓取脚本,它收集了数千个数据。问题是当出现错误时它会一直停止。我想要它只是记录错误并继续下一个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

如何防止错误停止脚本并记录?

1 个答案:

答案 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')