[英]JSON in post request works in HttpRequester but not in python Requests
我陷入了使用Python抓取網頁的網頁。 基本上,以下是來自HttpRequester(在Mozilla中)的請求,它給了我正確的響應。
POST https://www.hpe.com/h20195/v2/Library.aspx/LoadMore
Content-Type: application/json
{"sort": "csdisplayorder", "hdnOffset": "1", "uniqueRequestId": "d6da6a30bdeb4d77b0e607a6b688de1e", "test": "", "titleSearch": "false", "facets": "wildcatsearchcategory#HPE,cshierarchycategory#No,csdocumenttype#41,csproducttype#18964"}
-- response --
200 OK
Cache-Control: private, max-age=0
Content-Length: 13701
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sat, 28 May 2016 04:12:57 GMT
Connection: keep-alive
使用Requests在python 2.7.1中完全相同的操作失敗並出現錯誤。 以下是代碼段:
jsonContent = {"sort": "csdisplayorder", "hdnOffset": "1", "uniqueRequestId": "d6da6a30bdeb4d77b0e607a6b688de1e", "test": "", "titleSearch": "false", "facets": "wildcatsearchcategory#HPE,cshierarchycategory#No,csdocumenttype#41,csproducttype#18964"}
catResponse = requests.post('https://www.hpe.com/h20195/v2/Library.aspx/LoadMore', json = jsonContent)
以下是我得到的錯誤:
{"Message":"Value cannot be null.\r\nParameter name: source","StackTrace":" at
System.Linq.Enumerable.Contains[TSource](IEnumerable`1 source, TSource value, I
EqualityComparer`1 comparer)\r\n
更多信息:我正在尋找的Post請求被解雇:
打開此網頁: https : //www.hpe.com/h20195/v2/Library.aspx?doctype = 41&dococany = HEL&footer = 41&filter_doctype = no&filter_doclang = no&country =&filter_country = no&cc = us&lc = en&status = A&filter_status = rw#dococ -41&docsany -HPE&prodtype_oid-18964&狀態-A和排序順序,csdisplayorder和玩笑式及isRetired假&isRHParentNode假&titleCheck假
單擊頁面末尾的“加載更多”灰色按鈕
我正在從瀏覽器操作捕獲確切的請求標頭和響應集,並試圖在Postman,Python代碼和HttpRequester(Mozilla)中模仿它。
它使用Postman和Python標記了相同的錯誤(如上所述),但是使用HttpRequester時我沒有設置標題。
誰能想到對此的解釋?
如果郵遞員和requests
都收到錯誤,那么有比HttpRequester
顯示的更多的上下文 。 我希望幾乎總能設置一些標題,包括User-Agent
和Content-Length
,這里缺少這些標題。
通常的嫌疑人是cookie(在先前的請求中查找Set-Cookie
頭,通過使用requests.Session()
對象來保留它們), User-Agent
頭和可能是Referrer
頭,但是要查找其他頭文件,例如以例如, Accept
。
例如,將HttpRequester
發布到http://httpbin.org/post
,並檢查返回的JSON,它告訴您發送了哪些標頭。 這不包括cookie(這些是特定於域的),但其他任何東西都可能是服務器所尋找的東西。 如果cookie沒有幫助,請逐個嘗試這樣的標題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.