Python 如何在出现错误消息的情况下继续刮取

Python 如何在出现错误消息的情况下继续刮取,python,web-scraping,Python,Web Scraping,我正在运行我的网页抓取脚本,它收集了数千个数据。问题是当出现错误时,它会一直停止。我希望它只记录错误并继续下一个url。以下是我目前关于例外的情况 uClient3 = '' while uClient3 == '': try: uClient3 = requests.get(fsgsubcard2ref) print("Proceding to the next level in...") except: print("Connec

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

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

uClient3=requests.get(fsgsubcard2ref)
之后,
uClient3
将被存储
Response
对象,而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')

你的代码缩进得很厉害。
continue
语句属于哪里?除了之外,
是否确实按照您发布错误的方式缩进?并添加错误的堆栈跟踪。这是一个错误吗?你的try语句不应该退出循环我已经修复了indentation@DYZ
requests.get('https://stackoverflow.com)
return
Response
object,而不是
str
。和
而uClient3=''
由于
响应,每次都将
为False
fsgsubcars2ref似乎是一个url。因此,您将对url中的每个字符发出HTTP请求,每次都会失败并出现打印错误。我添加了一条注释,即
fsgsubcard2ref
应该是一个包含所有url的列表