python爬取post请求Reque Payload的json数据

时间:2022-10-23 19:49:56
import requests,json
url = "https://www.xxxxxxxx"
headers = {
'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Content-Type': 'application/json;charset=UTF-8',
'Host': 'www.xxxx.com',
'Referer': 'https://www.xxxx.com/',
'token': 'xxxxxxxxx',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
'Connection': 'keep-alive',
'Content-Length': '320',
'Origin': 'https://www.xxxxx.com',
}
payload = {"currentPage":1,"serviceTime":"2019-04-27 10:31:30","data":{"sonCategoryId":"3070","brandId":"","firstCategoryId":"","specialAreaId":"","categoryIds":["3070"],"brandIds":[],"isAscending":"","saleModel":-1,"searchModes":[],"sort":0,"shopId":"","currentPage":1,"pageSize":60},"pageSize":60,"addressId":1205414,"deviceType":3}
#原生的payload为{"currentPage":1,"data":{"sonCategoryId":"3070","brandId":"","firstCategoryId":"","searchKey":null,"specialAreaId":"","categoryIds":["3070"],"brandIds":[],"labelId":null,"isAscending":"","saleModel":-1,"searchModes":[],"sort":0,"shopId":"","currentPage":1,"pageSize":60},"pageSize":60,"addressId":1205414,"deviceType":3} *********************这里需要把value为“null”的元素去掉,例如labelId":null,和"searchKey":null,(这里需要特别注意)
response = requests.post(url=url,headers=headers,data=json.dumps(payload))
text = response.text
print(text)

  这里需要特别注意的是,把payload里面value为‘null’的值去掉(这里只是改url需要注意这一点,其他的还没测试),该url = “https://www.xxxxxxxxxxxxx”