相关疑难解决方法(0)

即使使用 User-Agent 标头,如何修复 Python 请求的“403 Forbidden”错误?

我正在向某个 URL 发送请求。我将curl命令复制到python中。因此,所有标头都包含在内,但我的请求不起作用,并且我在 HTML 输出中收到状态代码 403 和错误代码 1020。

代码是

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.5',
    # 'Accept-Encoding': 'gzip, deflate, br',
    'DNT': '1',
    'Connection': 'keep-alive',
    'Upgrade-Insecure-Requests': '1',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'none',
    'Sec-Fetch-User': '?1',
}

response = requests.get('https://v2.gcchmc.org/book-appointment/', headers=headers)

print(response.status_code)
print(response.cookies.get_dict())
with open("test.html",'w') as f:
    f.write(response.text)
Run Code Online (Sandbox Code Playgroud)

我也收到了 cookie,但没有得到所需的响应。我知道我可以用硒做到这一点,但我想知道这背后的原因。

注意:
我已经安装了所有库并检查了版本,但它仍然无法工作并抛出 403 错误。

python http-status-code-403 python-requests

6
推荐指数
1
解决办法
2万
查看次数