【问题标题】:Python 3 urllib.request capture and or stop redirectionPython 3 urllib.request 捕获和/或停止重定向
【发布时间】:2018-08-29 21:22:08
【问题描述】:

所以我使用下面的代码 sn-p 来捕获 URL 请求。问题是当重定向发生时,我需要一种方法来捕获重定向已经发生,或者完全停止重定向。现在,当我打印出代码时,我看到 200,因为那是重定向页面的代码。 geturl() 也为我提供了重定向页面的 url,但它不能帮助我解决重定向问题。

url = website + console
context = ssl._create_unverified_context()
response = urllib.request.urlopen(url, timeout=10, context=context)
code = response.getcode()
print(code)
print(response.geturl())

【问题讨论】:

    标签: python-3.x url-redirection urllib


    【解决方案1】:

    因此,我为此找到的最佳答案是更改我正在使用的库并使用 Requests。将重定向设置为 false 并适应需要忽略它们的 ssh 问题非常简单,就像 verify=false 一样简单。

    import requests    
    response = requests.get(url, verify=False, allow_redirects=False, timeout=5)
    

    【讨论】:

    • 我收到此错误 AttributeError: exit
    猜你喜欢
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2012-02-26
    • 2012-09-19
    • 2014-10-04
    • 2011-04-22
    相关资源
    最近更新 更多