Python爬取知乎回答信息碰到:Max retries exceeded with URL

那天我在爬取知乎图片的时候碰到了这个问题。

开始我以为程序逻辑出错了,折腾了很久,知乎现在要爬取回答下面所有信息的话,就得翻页了,而获取翻页以及更多的信息就得考虑异步加载。

然后在浏览器里面找到了下一页的url


其中,next就是下一页的url,previous就是上一页的url,total:518是问题下回答的总数。

估计知乎对这些url的访问做了限制,虽然我弄了代理,但还是碰到了这个问题。

解决办法如下:

在requests库获取html时,如果碰到访问不成功,则用try-except加上循环继续访问,并用sleep控制访问频率

html = ""
    while html == "":      #因为请求可能被知乎拒绝,采用循环+sleep的方式重复发送,但保持频率不太高
        try:
            proxies = get_random_ip(ipList)
            print("这次试用ip:{}".format(proxies))
            r = requests.request("GET", url, headers=headers, params=querystring, proxies=proxies)
            r.encoding = 'utf-8'
            html = r.text
            return html
        except:
            print("Connection refused by the server..")
            print("Let me sleep for 5 seconds")
            print("ZZzzzz...")
            sleep(5)
            print("Was a nice sleep, now let me continue...")
            continue

问题到这里应该就解决了。

参考:Max retries exceed with URL (需要翻墙)

猜你喜欢

转载自blog.csdn.net/Morzker/article/details/77428051
今日推荐