python - Python 请求给出 JSONDecodeError
问题描述
我有以下 curl 语句以 json 格式提供响应:
curl 'https://www.accenture.com/nl-en/searchbykeywords.search' \
-H 'authority: www.accenture.com' \
-H 'accept: application/json, text/javascript, */*; q=0.01' \
-H 'dnt: 1' \
-H 'x-requested-with: XMLHttpRequest' \
-H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36' \
-H 'content-type: application/json; charset=UTF-8' \
-H 'origin: https://www.accenture.com' \
-H 'sec-fetch-site: same-origin' \
-H 'sec-fetch-mode: cors' \
-H 'sec-fetch-dest: empty' \
-H 'referer: https://www.accenture.com/nl-en/search/results?srk=covid&pg=1&sb=0&filter=' \
-H 'accept-language: en-GB,en-US;q=0.9,en;q=0.8' \
-H 'cookie: eVar46=covid' \
--data-binary '{"k":"covid","f":1,"s":10, "sb":0, "ss":"" ,"cs":"true"}' \
--compressed
但是,当我在请求模块的帮助下尝试通过 Python 复制它时,我没有收到导致解码错误的 json。
import requests
with requests.Session() as session:
header = {
'authority': 'www.accenture.com',
'accept': 'application/json, text/javascript, */*; q=0.01' ,
'dnt': '1',
'x-requested-with': 'XMLHttpRequest',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36',
'content-type': 'application/json; charset=UTF-8' ,
'origin': 'https://www.accenture.com' ,
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'cors' ,
'sec-fetch-dest': 'empty',
'referer': 'https://www.accenture.com/nl-en/search/results?srk=covid&pg=1&sb=0&filter=',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
'cookie': 'eVar46=covid'
}
data = '{"k":"COVID","f":1,"s":10, "sb":0, "ss":"", "cs":"true"}'
search_url = 'https://www.accenture.com/nl-en/searchbykeywords.search'
r = session.post(search_url, headers=header, data=data)
data = r.json()
print(data)
你能帮我吗,因为可用的答案没有为我提供有效的解决方案。谢谢
解决方案
import requests
with requests.Session() as session:
header = {
'authority': 'www.accenture.com',
'accept': 'application/json, text/javascript, */*; q=0.01' ,
'dnt': '1',
'x-requested-with': 'XMLHttpRequest',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36',
'content-type': 'application/json; charset=UTF-8' ,
'origin': 'https://www.accenture.com' ,
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'cors' ,
'sec-fetch-dest': 'empty',
'referer': 'https://www.accenture.com/nl-en/search/results?srk=covid&pg=1&sb=0&filter=',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
'cookie': 'eVar46=covid'
}
data = {"k":"COVID","f":1,"s":10, "sb":0, "ss":"", "cs":"true"}
search_url = 'https://www.accenture.com/nl-en/searchbykeywords.search'
r = session.post(search_url, headers=header, json=data)
data = r.json()
print(data)
你可以在这里阅读更多 -更复杂的 POST 请求
推荐阅读
- delve - 无法再次停止 dlv 调试 --headless - 在 mac 上的 docker 容器中运行
- wpf - WPF - 仅在一个元素上使用控制模板
- groovy - 如何访问称为“属性”的公共类成员?
- python - 在 Django 上进行网站浏览时在后台执行脚本
- java - GraphView 中轴的最大值
- java - rxjava2 中 retrywhen() 发出的 observable 的用途是什么?
- docker - Intellij IDEA Docker 视图不显示日志
- c# - 在 ODL 文件中 - 在 coclass 中列出额外的接口是否必要或有用?
- c# - C#将绘制的椭圆的位置保存在列表中
? - android - Rating() 在“android.media.Rating”中不公开。无法从外包装评估