post-request中的JSON在HttpRequester中有效,但在python请求中无效



我一直在使用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

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.rnParameter name: source","StackTrace":"   at
System.Linq.Enumerable.Contains[TSource](IEnumerable`1 source, TSource value, I
EqualityComparer`1 comparer)rn   

更多信息:我正在寻找的Post请求被触发:

  1. 打开此网页:https://www.hpe.com/h20195/v2/Library.aspx?doctype=41&doccompany=HPE&页脚=41&filter_doctype=no&filter_doclang=no&country=&filter_country=no&cc=us&lc=en&status=A&filter_status=rw#doctype-41&文档公司HPE&prodtype_oid-18964&状态-a&排序次序csdisplayorder&挑逗关闭&isRetired false&isRHParentNode错误&title检查错误

  2. 点击";加载更多";页面末尾的灰色按钮

我正在从浏览器操作中捕获一组准确的请求头和响应,并试图在Postman、Python代码和HttpRequester(Mozilla)中模仿它。

它在Postman和Python中标记了相同的错误(如上所述),但在HttpRequester中没有设置任何头。

有人能想到对此的解释吗?

如果Postman和requests都收到错误,则上下文比HttpRequester显示的要多。我希望几乎总是设置许多标头,包括这里缺少的User-AgentContent-Length

通常可疑的是cookie(在早期请求中查找Set-Cookie标头,通过使用requests.Session()对象来保留这些标头)、User-Agent标头,也许还有Referrer标头,但要查找其他标头,例如以Accept开头的标头。

例如,让HttpRequester发布到http://httpbin.org/post,并检查返回的JSON,JSON会告诉您发送了哪些头。这不包括cookie(这些是特定于域的),但服务器可能会查找其他任何内容。如果cookie没有帮助,请逐个尝试这样的标题。

相关内容

最新更新