python - 响应正文始终为空,但响应代码为 200,同时从网络服务器获取 JSON 响应
问题描述
我正在尝试使用 python 的 requests 模块从特定链接(请参阅下面的 python 代码)获取 JSON 响应。当我在 Firefox 的 RESTer 中测试链接时(或者只是将其复制到浏览器的地址栏中),它会返回应有的信息:
fetchJSON_comment98({"productAttr":null,"productCommentSummary":{"skuId":100020974898,"averageScore":5,"defaultGoodCount":0,"defaultGoodCountStr":"10��+","commentCount":0," commentCountStr":"10��+","goodCount":0,"goodCountStr":"2.1��+","goodRate":0.97,"goodRateShow":97,"generalCount":0,"generalCountStr":" 200+","generalRate":0.02,"generalRateShow":2,"poorCoun ...(截断)
标题:
- 日期:格林威治标准时间 2021 年 6 月 9 日星期三 09:25:31
- 内容类型:text/html;charset=GBK
- 传输编码:分块
- 连接:关闭
- 变化:接受编码
- 设置 Cookie:JSESSIONID=502398ABD60D51F774B1E90EEF32F818.s1;路径=/jwotest_product=99; 域名=club.jd.com;过期=格林威治标准时间 2021 年 6 月 16 日星期三 09:25:30;路径=/
- 服务器:jfe
- 严格的传输安全性:max-age=7776000
同样在 Firefox 的网络检查器中显示: Firefox Network Inspector
但是当我从 python 3.7 尝试以下代码时:
from requests import Session
url = "https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=100020974898&score=0&sortType=6&page=0&pageSize=10&isShadowSku=0&fold=1"
headers = {"Host": "club.jd.com",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
"User-Agent": "Mozilla/5.0"}
s = Session()
resp = s.get(url=url, headers=headers)
print(resp.text)
我收到一个 HTTP 200 响应和一个空响应正文,其中包含以下响应标头:
'date' (1890118560096) = {tuple} <class 'tuple'>: ('Date', 'Wed, 09 Jun 2021 09:33:02 GMT')
'content-type' (1890118524080) = {tuple} <class 'tuple'>: ('Content-Type', 'text/html;charset=GBK')
'transfer-encoding' (1890118269376) = {tuple} <class 'tuple'>: ('Transfer-Encoding', 'chunked')
'connection' (1890118524464) = {tuple} <class 'tuple'>: ('Connection', 'close')
'vary' (1890118560376) = {tuple} <class 'tuple'>: ('Vary', 'Accept-Encoding')
'content-encoding' (1890118568528) = {tuple} <class 'tuple'>: ('Content-Encoding', 'gzip')
'server' (1890118560880) = {tuple} <class 'tuple'>: ('Server', 'jfe')
'strict-transport-security' (1890118632112) = {tuple} <class 'tuple'>: ('Strict-Transport-Security', 'max-age=7776000')
我曾尝试使用 CookieJar 添加 cookie,或者从浏览器的响应中复制它,或者自己制作,但这些都不起作用。尝试了 Stackoverflow 上列出的许多解决方案,但没有成功...
请帮助我,我做错了什么?
解决方案
问题在于用户代理标头。将标题更改为浏览器中的任何内容,并且代码可以正常工作。您可以在此处阅读有关用户代理标头格式的更多信息: https ://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
from requests import Session
url = "https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=100020974898&score=0&sortType=6&page=0&pageSize=10&isShadowSku=0&fold=1"
headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"}
s = Session()
resp = s.get(url=url, headers=headers)
print(resp.text)
推荐阅读
- excel - 当单元格/范围中的值更改时执行过程
- maven - gradle 项目中的存储库未编入索引
- flutter - 在 main.dart 中使用 Navigator 获取当前上下文
- javascript - 我们可以在不同设备上的两个浏览器之间使用网络蓝牙来聊天或交换数据吗?
- angular - ngIf 在另一个元素之前渲染元素
- javascript - 如何在同一个函数中进行 100 次调用,以迭代 api 端点?[Pokeapi]
- python - /email-signup 中的 TypeError 类型为“WSGIRequest”的对象不是 JSON 可序列化的
- python - python 简单的套接字客户端/服务器示例
- swift - 如何回到之前的场景并重新加载数据?
- python - 为什么更改矩阵的元素会同时更改两行?