[英]Requests failed to decode response
我想使用 python 请求从 API 获取响应。 在邮递员中这是有效的。 我使用标题Content-Type application/json
和正文原始 JSON。 我怎么能在python中做到这一点?
现在我得到以下contentDecodingError
'Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing data: incorrect data check')
示例代码
import requests
url = "http://api.semstorm.com/api-v3/monitoring/monitoring-keyword/get-list"
key = "xxxx"
dat = {'services_token' : key, 'campaign_id' : 43174}
hed = {'Content-Type': 'application/json'}
POST = requests.post(url, json = dat, headers = hed)
您的模块需要gzip但服务器不会返回gzip。 您可以尝试添加编码类型。 我无法保证类型,这取决于你的api。
import requests
url = "http://api.semstorm.com/api-v3/monitoring/monitoring-keyword/get-list"
key = "xxxx"
dat = {'services_token' : key, 'campaign_id' : 43174}
hed = {'Content-Type': 'application/json', 'Accept-Encoding': 'deflate'}
POST = requests.post(url, json = dat, headers = hed)
如果我添加encode = c('json'),这对我来说是有效的。
你能在python层面解决问题吗? 因为我只能下载关于活动的基本数据,但它不像在 php 中那样向我显示数据
import requests
import urllib3
import json
urllib3.disable_warnings()
import zlib
import gzip
url = 'http://api.semstorm.com/api-v3/monitoring/monitoring-campaign/'
headers = {
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip',
}
campaign_id = xxxxx
hed = {'Content-Type': 'application/json', 'Accept-Encoding': 'deflate'}
data = {
'services_token': 'xxxxx',
'campaign_id':campaign_id,
"datemin": "20211101",
"datemax": "20211130",
"gap": "monthly"
}
url_new = url + str(campaign_id) +'.json'
from pandas import json_normalize
r= requests.get(url_new, headers=headers, json = data, allow_redirects=False, verify=False,params=data
)
jsonResponse = json.loads(r.text)
json_normalize(jsonResponse['results'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.