我正在尝试发送ajax请求并获取json数据,但我却收到了400错误的请求
我尝试传递不同的标头,但仍然无法正常工作
import requests
import json
headers = {"Host": "www.zalando-prive.it",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0",
"Accept": "application/json, text/plain, */*",
"Accept-Language": "en-US",
"Accept-Encoding": "gzip, deflate, br",
"X-Requested-With": "XMLHttpRequest",
"Referer":"https: //www.zalando-prive.it/campaigns/ZZLPH1"
}
data = {"filter": {},
"sort": "attractivity",
"gender": "FEMALE",
"page": 1}
url = "https://www.zalando-prive.it/api/campaigns/ZZLPH1/articles"
response = requests.get(url, data=data, headers=headers)
print(response.text)
您的浏览器发送了一个该服务器无法理解的请求,而我希望得到json响应
答案 0 :(得分:0)
如果您提供了更多有关我只能猜测您要做什么的信息,将会有所帮助。
您可以按照注释中的建议将data=data
更改为params=data
。这将给出一个html响应,而不是一个json响应(如果您提供了更多信息,我们可以调试此问题)。
在html源代码中,有一个json响应,但是需要通过一些字符串操作将其取出,然后进行解码。完成后,您就可以使用json.loads()
来做到这一点:
import requests
import json
from urllib.parse import unquote
headers = {"Host": "www.zalando-prive.it",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0",
"Accept": "application/json, text/plain, */*",
"Accept-Language": "en-US",
"Accept-Encoding": "gzip, deflate, br",
"X-Requested-With": "XMLHttpRequest",
"Referer":"https://www.zalando-prive.it/campaigns/ZZLPH1"
}
data = {"filter": {},
"sort": "attractivity",
"gender": "FEMALE",
"page": 1}
url = "https://www.zalando-prive.it/api/campaigns/ZZLPH1/articles"
response = requests.get(url, params=data, headers=headers)
print(response.text)
jsonStr = response.text
jsonStr = jsonStr.split('data-cms-content="')[-1]
jsonStr = jsonStr.split('" data-reactroot="">')[0]
jsonData = json.loads(unquote(jsonStr))