Python在web抓取过程中请求过多的重定向?

2024-04-20 13:26:07 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我想从网上抓取一个站点,但是当我在几个请求之后(大约30个最大值)遍历结果页面时请求.get引发此错误:

requests.exceptions.TooManyRedirects: Exceeded 30 redirects

搜索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))

Tags: importurl代理gettime站点错误random