post-request中的JSON在httprequest中起作用,但在python请求中不起作用

2024-05-15 23:58:25 发布

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

我被困在使用Python进行网页抓取。基本上,它在下面的请求中给出了htpilla的响应。在

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

Python2.7.1中使用请求的完全相同的操作失败,并出现错误。以下是代码片段:

^{pr2}$

以下是我得到的错误:

{"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请求被触发:

  1. opening this web page: https://www.hpe.com/h20195/v2/Library.aspx?doctype=41&doccompany=HPE&footer=41&filter_doctype=no&filter_doclang=no&country=&filter_country=no&cc=us&lc=en&status=A&filter_status=rw#doctype-41&doccompany-HPE&prodtype_oid-18964&status-a&sortorder-csdisplayorder&teasers-off&isRetired-false&isRHParentNode-false&titleCheck-false

  2. Clicking on the "Load more" grey button at the end of the page

我从浏览器操作捕获请求头和响应的确切集合,并尝试在Postman、Python代码和HttpRequester(Mozilla)中模拟这些内容。在

它用Postman和Python标记相同的错误(上面提到过),但是在HttpRequester中没有设置头。在

有人能想出一个解释吗?在


Tags: thenohttpscomfalsewwwstatus错误
1条回答
网友
1楼 · 发布于 2024-05-15 23:58:25

如果邮递员和requests都收到了错误,那么上下文就比HttpRequester显示的内容要多。我希望几乎总是设置一些头,包括User-Agent和{},它们在这里丢失了。在

常见的疑点是cookies(在早期的请求中查找Set-Cookie头,通过使用requests.Session()对象来保存这些头)、User-Agent头和可能是Referrer头,但是要查找其他头,例如以Accept开头的任何头。在

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

相关问题 更多 >