【问题标题】:Python requests wait for redirectPython 请求等待重定向
【发布时间】:2021-09-19 09:38:26
【问题描述】:

我正在尝试访问一个网站,该网站通过在几秒钟后重定向来验证我不是机器人。如何让请求模块等待重定向?

编辑:看来我没有完全理解这个问题。我喜欢 Kirk Strauser 的回复,但找不到位置标题。

我发现该站点是使用 Cloudflare 管理的。我尝试使用 cfscrape,但没有成功,并且 cloudcraper 尚未针对此验证码版本进行更新。在stackexchange上可能不会解决。

【问题讨论】:

    标签: python python-requests bots


    【解决方案1】:

    requests模块有一个参数用于配置allow_redirects,如果你需要跟随重定向的300系列响应。

    https://docs.python-requests.org/en/master/user/quickstart/#redirection-and-history

    >>> r = requests.get('http://github.com/', allow_redirects=False)
    
    >>> r.status_code
    301
    
    >>> r.history
    []
    

    【讨论】:

      【解决方案2】:

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

      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'])
      

      【讨论】:

        猜你喜欢
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 2017-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-27
        相关资源
        最近更新 更多