【问题标题】:JSON in post request works in HttpRequester but not in python Requests发布请求中的 JSON 在 HttpRequester 中有效,但在 python 请求中无效
【发布时间】:2016-09-26 11:47:38
【问题描述】:

我被困在使用 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 中使用请求的完全相同的操作失败并出现错误。以下是sn -p的代码:

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

  1. 打开此网页: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. 点击页面末尾的“加载更多”灰色按钮

我正在从浏览器操作中捕获确切的请求标头和响应集,并尝试在 Postman、Python 代码和 HttpRequester (Mozilla) 中进行模仿。

它使用 Postman 和 Python 标记相同的错误(如上所述),但在我没有使用 HttpRequester 设置标头的情况下工作。

谁能想到一个解释?

【问题讨论】:

  • 可能是 HttpRequester 发送了一个 cookie,或者服务器根据用户代理改变了行为。无法判断,但您的 requests 代码在其他方面是正确的。
  • 感谢您的快速回复。但是如果 HttpRequest 发送一个 cookie,它应该被列为请求标头的一部分,对吗?在原始输出(在我的问题中列出)中,除了 content-type 之外,我没有看到任何标题。我不认为用户代理是问题,因为用户代理“User-Agent:python-requests/2.10.0”适用于对同一服务器的不同发布请求。
  • HttpRequester 输出中缺少的标头太多;没有内容长度,没有接受,没有用户代理。您不会看到所有发送的标头,因此您无法做出任何假设。
  • 该问题已通过使用请求会话解决,该会话创建了一个在不同 Post 请求之间持续存在的 cookie ASP.NET_SessionId。 Martijn 是对的 - 看起来 HttpRequester 传递的标头比原始输出中明显的要多。另一个提示来自 Postman - 当我启用拦截器并使用浏览器的 cookie(其中包含会话 ID 等)时,Post 请求通过了。

标签: python json post web-scraping python-requests


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2020-07-26
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 2014-09-18
    相关资源
    最近更新 更多