【问题标题】:Page is giving 403 response when tried to get the data页面在尝试获取数据时给出 403 响应
【发布时间】:2019-11-19 17:41:00
【问题描述】:

我正在尝试使用 Python(2.7) 请求模块请求此页面 https://health.usnews.com/best-hospitals/rankings/cancer。但它给出了 403 响应(它在我的本地机器上工作正常,但在服务器上却不工作)。

请求页面绕过请求中的标头和 cookie。但是得到了 403 响应。此外,尝试了Session 对象以及Python requests - 403 forbidden - despite setting `User-Agent` headers 中的建议

>>> requests.get('https://health.usnews.com/best-hospitals/rankings/cancer')
<Response [403]>
>>> requests.get('https://health.usnews.com/best-hospitals/rankings/cancer', headers=h)
<Response [403]>

我们如何从该页面获得正确的响应?

提前谢谢你!

【问题讨论】:

    标签: python python-requests


    【解决方案1】:

    发出请求时需要标头中的User-Agent

    import requests
    
    url = 'https://health.usnews.com/best-hospitals/rankings/cancer'
    headers = {'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0'}
    
    txt = requests.get(url, headers=headers).text
    print(txt)
    

    打印:

    <!doctype html>
    <html class="no-js" lang="">
        <head>
    ... and so on.
    

    【讨论】:

    • 谢谢安德烈!是的,我也试过了。它在我的本地机器上工作。当我从服务器尝试相同时,它会给出 403 响应。忘了提这个问题。会更新那个。对此有什么想法吗?
    • @slipa 你在使用代理吗?也许你的 IP 被列入黑名单,所以你想尝试不同的 IP。
    猜你喜欢
    • 2017-01-04
    • 1970-01-01
    • 2021-02-27
    • 2021-06-17
    • 2021-09-03
    • 2018-09-11
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    相关资源
    最近更新 更多