2017-10-12 138 views
1

我想從使用AJAX的網站獲取信息。當我使用REST插件進行chrome時,我可以發送請求並接收所需的答案。我想編寫一個python腳本,但我堅持如何以正確的格式編寫請求。目前,我收到了未經授權的頁面響應,我認爲這是由於未使用XHR發送它?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擴展I型:

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

這給了我希望的結果。

所需的輸出:

{ 
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 
} 
} 
}` 

回答

1

你只是缺少一個標題:

'X-Requested-With': 'XMLHttpRequest' 

更新代碼:

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", 'X-Requested-With': 'XMLHttpRequest'}) 

    print(r.content) 

而且,通常CSRF令牌的變化。 因此,您應該檢索HTML,獲取CSRF令牌,然後執行您的XHR POST請求。

我會推薦beautifulsoup在HTML中查找CSRF標記。

+0

運行修改後的代碼:'code:b'{「status」:400,「errorCode」:1002,「fail」:「無法獲取在線定價和可用性信息」,「錯誤」: [{「errorCode」:0,「message」:「products:json input missing」,「constraints」:{},「errorcode」:0}'... – Aiden

+0

此外,我會自動化令牌和存儲號碼一次我可以讓請求工作。謝謝您的幫助! – Aiden

+0

這個錯誤是由於你沒有發送好的CSRF令牌恕我直言。 您知道CSRF令牌是一種安全措施,如果您不發送好的請求,您的請求將始終失敗。 –