如何在 python 中向 Graphql 发送 post 请求

How to send a post requests to Graphql in python

我想抓取这个网站 https://www.weisse-liste-pflege.de/ 如果我们在搜索栏中像 berlin 一样搜索并打开开发人员工具,并在网络选项卡中向 Graphql 发出 post 请求,它包含 JSON 中的所有数据,我想要该数据。 这是我的代码。

import requests
url = 'https://www.weisse-liste-pflege.de/graphql'
r = requests.post(url)
r
<Response [500]>

请指导我如何实现 json 数据

您需要将在 Request Payload 部分中看到的 payload 作为请求中的 json 参数传递。

import requests

data = {"operationName":"getAdvisoryOfficeDetails","variables":{"lat":"52.51767","lon":"13.40553"},"query":"query getAdvisoryOfficeDetails($lat: String, $lon: String) {\n  advisoryOffice(lat: $lat, lon: $lon) {\n    status\n    msg\n    href\n    result_count\n    data {\n      address\n      city\n      contact {\n        contact_name\n        email\n        telephone\n      }\n      distance\n      href\n      id\n      name\n      provider\n      state\n      updated\n      url\n      zip\n    }\n  }\n}\n"}
response = requests.post('https://www.weisse-liste-pflege.de/graphql', json=data)

print(response.json())
# {"data":{"advisoryOffice":{"status":200,"msg":"Request successful.","href":"https://www.zqp.de/beratung-pflege/?lat=52.51767&lng=13.40553&filter=3+13+14","result_count":1,"data":[{"address":"Fischerinsel 3","city":"Berlin","contact":[{"contact_name":null,"email":null,"telephone":"030-20454615"}],"distance":"0.5324999652222573","href":null,"id":"BE-0143","name":"Rat & Tat für Ältere Menschen","provider":"Mehrgenerationenhaus Berlin Mitte - KREATIVHAUS e.V.","state":"BE","updated":"2018-02-19 12:53:13","url":"http://www.kreativhaus-berlin.de/web/rat-tat","zip":"10179"}]}}}

To get data from other locations, you can change coordinates {"lat":"52.51767","lon":"13.40553"}