繁体   English   中英

Python在网络抓取过程中请求太多重定向?

[英]Python requests Too Many Redirects during web scraping?

所以我想通过Web抓取一个站点,但是当我在几个请求(最多约30个)requests.get之后迭代结果页面时,抛出此错误:

request.exceptions.TooManyRedirects:超过30个重定向

搜索URL重定向到主页URL,然后每个下一个URL都作用相同,直到我连接到不同的VPN。 即使当我欺骗用户代理并从免费代理列表中轮换代理时,它仍然会在几次请求后被重定向。 我以前从未像以前那样在网络抓取过程中遇到问题。 绕过此“重定向块”的最佳方法是什么? allow_redirects = False在这里也不起作用。

import requests
import random
import time

agents = [...] # List of user agents

for i in range(1,100):
    url = "https://panoramafirm.pl/odpady/firmy,{}.html".format(i)
    r = requests.get(url, headers={"User-Agent": random.choice(agents)})
    print(r.status_code)
    time.sleep(random.randint(10,15))

由于您正在使用requests ,因此可以使用allow_redirects=False选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM