python-requests - 带有 Python 获取请求的莫名其妙的 403 错误
问题描述
我正在尝试做一个简单的获取请求,但无论我如何配置标头,我都会收到 403 响应。该页面在浏览器中加载良好。不需要登录,也没有跟踪的 cookie。我试图从中获得响应的链接如下,后面是我的简单代码。
url = 'https://i7.sportsdatabase.com/nba/query.json?sdql=50+%3C+Kobe+Bryant%3Apoints+and+site%3Daway&sport=nba'
headers = {
'Host': 'i7.sportsdatabase.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',
}
r = requests.get(url, headers)
我没有看到需要添加到请求中的任何其他标头。浏览器中完整的请求标头如下:
Host: i7.sportsdatabase.com
Connection: keep-alive
Cache-Control: max-age=0
sec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"
sec-ch-ua-mobile: ?0
DNT: 1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: en,en-US;q=0.9,it;q=0.8,es;q=0.7
If-None-Match: "be833f0fb26eb81487fc09e05c85ac8c8646fc7b"
解决方案
尝试:
- 将您的 URL 设为字符串
- 添加接受
这有效:
url = 'https://i7.sportsdatabase.com/nba/query.json?sdql=50+%3C+Kobe+Bryant%3Apoints+and+site%3Daway&sport=nba'
headers = {
'Host': 'i7.sportsdatabase.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
}
r = requests.get(url, headers=headers)
推荐阅读
- amazon-web-services - AWS 多 DNS A 记录创建
- sql-server - SQL Server XML 命名空间冒号
- android - 如何使用 Volley 在 GET 请求中发送正文
- grpc - 在 K6 中,我们可以在不指定端口号的情况下连接到 GRPC 服务吗?
- java - 如何从数据库中获取 300 万条记录并生成 CSV 文件
- docker - Dockerfile dotnet/sdk/5.0 错误 - container_linux.go:380:启动容器进程导致:exec:“cmd”:$PATH 中找不到可执行文件
- javascript - 从 d3.js 中的 json API 绘制地图?
- javascript - DOMContentLoaded 将在我的页面上触发两次
- javascript - 无法暂停动画 SVG
- kotlin - 期望成员声明(当使用来自另一个类的变量时会发生这种情况)