Python XHR请求格式化

2024-09-29 01:24:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用AJAX从一个网站获取信息。当我使用chrome的REST插件时,我可以发送请求并接收所需的答案。我想写一个python脚本,但是我不知道如何以正确的格式编写请求。目前我收到一个未经授权的页面响应,我认为这是由于没有使用XHR发送它?在

代码:

import json
import requests

payload = {"stores":"3650","products":{"31445761":[{"sku":"6000197536050","upc":["4549659075"]}]},"origin":"pip","csrfToken":"d707af2ed8b79a78a669b38dff593c909f6b6262-1507764346644-ebc72845dfa30431a8f7b1c4"}

with requests.Session() as session:
    session.get("https://www.walmart.ca")
    r = session.post('https://www.walmart.ca/ws/en/products/availability', data=json.dumps(payload),
                 headers={"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"})

    print(r.content) 

当我在REST Chrome扩展中输入:

^{pr2}$

这给了我想要的结果。在

期望输出:

{
31445761: {
"online": [
  {
"maxRegularPrice": 0,
"minRegularPrice": 0,
"mapPrice": 0,
"minCurrentPrice": 99.96,
"maxCurrentPrice": 99.96,
"inventory": 0,
"sku": "6000197536050",
"clearance": false,
"offerId": "6000197536050",
"limited": false,
"reducedPrice": false,
"offerType": "1P",
"limitedStock": false,
"sellerId": "0",
"rollback": false,
"date": "",
"status": "OutOfStock",
"eligible": false,
"sellerName": "Walmart",
"asAdvertised": false
}
],
"stores": [
  {
"minRegularPrice": 0,
"maxRegularPrice": 0,
"minCurrentPrice": 99.96,
"maxCurrentPrice": 99.96,
"inventory": 0,
"sku": "6000197536050",
"clearance": false,
"limited": false,
"limitedStock": false,
"rollback": false,
"date": "",
"status": "OutOfStock",
"storeNumber": "3650",
"eligible": false,
"asAdvertised": false
}
],
"onlineSummary": {
"status": "OutOfStock",
"date": "",
"eligible": false,
"clearance": false,
"rollback": false,
"asAdvertised": false,
"limited": false,
"limitedStock": false,
"minRegularPrice": 0,
"maxRegularPrice": 0,
"minCurrentPrice": 99.96,
"maxCurrentPrice": 99.96
},
"storeSummary": {
"status": "OutOfStock",
"date": "",
"eligible": false,
"clearance": false,
"rollback": false,
"asAdvertised": false,
"limited": false,
"limitedStock": false,
"minRegularPrice": 0,
"maxRegularPrice": 0,
"minCurrentPrice": 99.96,
"maxCurrentPrice": 99.96
}
}
}`

Tags: falsedatestatusclearancelimitedskurollbackeligible
1条回答
网友
1楼 · 发布于 2024-09-29 01:24:25

你只是少了一个标题:

'X-Requested-With': 'XMLHttpRequest'

更新代码:

^{pr2}$

而且,通常CSRF令牌会发生变化。 所以您应该检索HTML,获取CSRF令牌,然后执行xhrpost请求。在

我建议beautifulsoup在HTML中查找CSRF令牌。在

相关问题 更多 >