【问题标题】:python requests <Response [520]>python请求<响应[520]>
【发布时间】:2017-09-22 19:34:54
【问题描述】:

我写了这个简单的python代码:

>>> import requests
>>> r = requests.get("http://prnt.sc/")
>>> r.status_code
520

我想在"http://prnt.sc/" 下载该页面,我可以使用我的浏览器 (Mozilla) 正确地对其进行可视化,但使用 python 我真的不能。

我已经尝试使用名为 fake_useragent 的 python 模块更改和随机化我的用户代理,但没有任何变化。

我认为这个问题可能是由于 python 采用的 traceroute 与我的浏览器不同。

如果我打印 r.text 我会收到 cloudflare 标准页面错误。

【问题讨论】:

    标签: python python-2.7 http python-requests


    【解决方案1】:

    包含一个真正的用户代理为我解决了这个问题:

    >>> requests.get("http://prnt.sc/", headers={'User-Agent': 'Mozilla/5.0 (Platform; Security; OS-or-CPU; Localization; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)'}).status_code
    200
    

    【讨论】:

    • 用户代理可以更简单:headers={'User-Agent': 'Chrome'}headers={'User-Agent': 'hello'} 也可以。
    猜你喜欢
    • 1970-01-01
    • 2020-08-04
    • 2021-10-01
    • 1970-01-01
    • 2014-05-07
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多