techiall / Blog

🍋 [My Blog] See discussions
https://github.com/techiall/Blog/discussions
MIT License
8 stars 1 forks source link

Python 爬虫 302 重定向 #40

Open techiall opened 5 years ago

techiall commented 5 years ago

python 中的 requests 是会跟着重定向,要想关闭自动重定向,需要在请求中关闭 allow_redirects=False 即可。

    r = requests.post(
        url=url,
        allow_redirects=False
    )
    print(r.headers['Location'])

参考代码如下

    r = requests.post(
        url=url,
        allow_redirects=False
    )
    print(r.headers['Location'])
    r_new = requests.get(
        url=new_url,
        headers={
            "Referer": "".format(r.headers['Location']),
            "Host": host
        }
    )