【问题标题】:Why am I getting a [403] Error when trying to use requests.get in Python为什么在 Python 中尝试使用 requests.get 时会出现 [403] 错误
【发布时间】:2020-05-25 03:40:03
【问题描述】:

我是 python 新手,正在尝试构建数据抓取工具。

尝试运行时:

schedule_data = requests.get('https://nsmmhl.goalline.ca/schedule.php?league_id=264&from_date=2019-09-12&to_date=2020-02-02')

我明白了:

<bound method Response.raise_for_status of <Response [403]>>

谁能告诉我为什么?该链接工作正常。

【问题讨论】:

    标签: python python-3.x web-scraping jupyter-notebook anaconda


    【解决方案1】:

    设置请求的头部属性

    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
        'AppleWebKit/537.36 (KHTML, like Gecko) '
        'Chrome/80.0.3987.149 Safari/537.36'
    }
    schedule_data = requests.get(
        'https://nsmmhl.goalline.ca/schedule.php?league_id=264&from_date=2019-09-12&to_date=2020-02-02',
         headers=headers
    )
    

    【讨论】:

    • 这成功了!你能告诉我为什么/我需要这样做吗?如果我遇到同样的问题,将来要检查什么?
    猜你喜欢
    • 1970-01-01
    • 2013-03-07
    • 2023-02-05
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多