【问题标题】:How to scrape non restful API using python requests?如何使用 python 请求抓取非 restful API?
【发布时间】:2021-07-31 23:41:27
【问题描述】:

我正在尝试抓取这个website。我从 Google Developer Tools 的 Network 标签中发现,对 URL https://tncovidbeds.tnega.org/api/hospitals 的名为 hospitals 的请求得到了我需要的响应。

我尝试在我的 python 代码中使用相同的标头和有效负载重新创建相同的情况,但得到的响应与网站不同。

这是我的python代码:

import requests

url = r'https://tncovidbeds.tnega.org/api/hospitals'

d = {
"searchString":"",
"sortCondition":{"Name":1},
"pageNumber":1,
"pageLimit":10,
"SortValue":"Availability",
"Districts":["5ea0abd3d43ec2250a483a4f"],
"BrowserId":"b4c5b065a84c7d2b60e8b23d415b2c3a",
"IsGovernmentHospital":"true",
"IsPrivateHospital":"true",
"FacilityTypes":["CHO","CHC","CCC"]
}

h = {
"authority": "tncovidbeds.tnega.org",
"method": "POST",
"path":"/api/hospitals",
"scheme": "https",
"accept": "application/json, text/plain, */*",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
"cache-control": "no-cache",
"content-length": "280",
"content-type": "application/json;charset=UTF-8",
"cookie": "_ga=GA1.2.1066740172.1620653373; _gid=GA1.2.1460220464.1620653373",
"origin": "https://tncovidbeds.tnega.org",
"pragma": "no-cache",
"sec-ch-ua": '" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"',
"sec-ch-ua-mobile": "?0",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"token": "null",
}

res = requests.post(url, data=d, headers=h)
print(res.json())

我得到的输出是:

{
'result': None,
 'exception': '',
 'pagination': None,
 'statusCode': '500',
 'errors': [],
 'warnings': []
}

我需要的响应和来自 Google 网络标签的响应是:

{
"result": A BIG LIST OF JSON OBJECTS,
"exception":null,
"pagination":{"pageNumber":1,"pageLimit":10,"skipCount":0,"totalCount":155},
"statusCode":"200",
"errors":[],
"warnings":[]}

你能建议我一个解决方案吗?

提前致谢。

【问题讨论】:

  • 我收到回复 无法获取 /hospitals 更新您的问题?
  • 如果您将 URL 复制粘贴到浏览器中,您当然会得到一个没有响应的空白页面,因为必须将 http 请求发送到带有 JSON 对象的 URL 才能获得响应。仅仅导航到路径是行不通的。回答 Gowtham Sooraraj ????在 cmets 中。

标签: python python-3.x web-scraping python-requests data-mining


【解决方案1】:

从您的浏览器请求中可以看出,content-type 必须是 application/json;charset=UTF-8。当传递一个有效载荷作为data 参数请求will create 一个application/x-www-form-urlencoded 请求。要解决此问题,您需要将有效负载作为 json 参数传递。它会自动设置正确的content-type

requests.post(url, json=d)

在您的情况下,您也不需要为请求提供任何额外的标头。

【讨论】:

    猜你喜欢
    • 2013-06-22
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多