【问题标题】:Scraping from frames, POST request not working in Python从帧中抓取,POST 请求在 Python 中不起作用
【发布时间】:2019-06-27 09:21:45
【问题描述】:

我正在开展一个从零件目录中获取价格的项目。由于它使用框架,我一直在使用 Chrome 开发工具来查看发送到服务器的内容。

我找到了获取数据的请求,但我似乎无法让它在 python 中工作。我还尝试通过复制 cURL 数据使用 Postman 发出请求,但它也不起作用。

这是我正在使用的代码:

headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-US,en;q=0.9",
    "Cache-control": "no-cache,no-store",
    "Connection": "keep-alive",
    "Content-Length": "205",
    "Content-Type": "application/json",
    "Cookie": "COOKIE HERE",
    "Expires": "0",
    "Host": "partscatalog.deere.com",
    "Origin": "https://partscatalog.deere.com",
    "Pragma": "no-cache",
    "Referer": "https://partscatalog.deere.com/jdrc/navigation/equipment/69779",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
    "X-TS-AJAX-Request": "true"
}

payload = {'{"eq":"69779","fr":{"businessRegion":1061,"filtersEnabled":"true","encodedFilters":null,"filteringLevel":null,"currentPin":null,"equipmentRefId":"69779"},"sp":"","br":"1061","locale":"en-US","vm":"p","ln":1}'}

r = requests.post("https://partscatalog.deere.com/jdrc-services/v1/navigation/p/getChildren", headers=headers, data=payload)
print(r.content)

提前致谢

【问题讨论】:

  • 有时您必须先获取普通页面才能获取新的 cookie 和会话 ID。
  • 如果您发送application/json,那么您可能必须使用json= 而不是data=
  • @furas 特别感谢 furas,因为这是主要问题之一,我还必须对有效负载进行更改。

标签: python http iframe python-requests


【解决方案1】:

你的代码有一些问题,首先我建议你用这个替换你的标题:

headers = {
    "Accept": "application/json, text/plain, */*",
    "Content-Type": "application/json",
    "Cookie": "COOKIE HERE",
    "Referer": "https://partscatalog.deere.com/jdrc/navigation/equipment/69779",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
    "X-TS-AJAX-Request": "true"
}

那么我也建议更换这个:

r = requests.post("https://partscatalog.deere.com/jdrc-services/v1/navigation/p/getChildren", headers=headers, data=payload)

与:

r = requests.post("https://partscatalog.deere.com/jdrc-services/v1/navigation/p/getChildren", headers=headers, json=payload)

希望这可行

【讨论】:

  • 感谢您的帮助。它奏效了,但我必须再做 1 次更改。我将有效负载中的 null 视为字符串,但我必须将其设置为 Python 等效于 null、None。我还必须将键“ln”和“businessRegion”的值设为整数而不是字符串。跨度>
猜你喜欢
  • 2016-03-27
  • 2016-08-11
  • 2022-06-10
  • 1970-01-01
  • 2018-04-16
  • 2012-12-29
  • 2014-11-01
  • 2017-11-09
  • 2014-09-16
相关资源
最近更新 更多