python请求等待重定向

v440hwme  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(201)

我正在尝试访问一个网站,通过几秒钟后重定向来验证我不是机器人。如何让请求模块等待重定向?
编辑:看来我没有完全理解这个问题。我喜欢kirk strauser的回答,但找不到位置标题。
我发现该站点是用cloudflare管理的。我尝试使用cfscrape,但没有成功,cloudscraper还没有更新这个验证码版本。可能不会在stackexchange上解决。

qmelpv7a

qmelpv7a1#

这个 requests 模块有一个用于配置的参数 allow_redirects 如果您需要遵循重定向的300系列响应。
https://docs.python-requests.org/en/master/user/quickstart/#redirection-历史

>>> r = requests.get('http://github.com/', allow_redirects=False)

>>> r.status_code
301

>>> r.history
[]
rjee0c15

rjee0c152#

在这种情况下,您可以先睡一会儿,然后手动访问下一个位置:

import time

import requests

r = requests.get('http://github.com/', allow_redirects=False)

# Don't fetch the next page too quickly

time.sleep(3)

# Now go to the URL that the previous request would have gone to

# if you hadn't asked requests.get not to.

r = requests.get(r.headers['Location'])

相关问题