首页 > 解决方案 > 响应正文始终为空,但响应代码为 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 ...(截断)

标题:

同样在 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 上列出的许多解决方案,但没有成功...

请帮助我,我做错了什么?

标签: pythonresthttp

解决方案


问题在于用户代理标头。将标题更改为浏览器中的任何内容,并且代码可以正常工作。您可以在此处阅读有关用户代理标头格式的更多信息: 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)

推荐阅读