首页 > 解决方案 > 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)

你能帮我吗,因为可用的答案没有为我提供有效的解决方案。谢谢

标签: pythonpython-requests

解决方案


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 请求


推荐阅读