【问题标题】:How to send a post requests to Graphql in python如何在 python 中向 Graphql 发送帖子请求
【发布时间】:2021-05-23 19:34:17
【问题描述】:

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

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

请指导我如何实现 json 数据

【问题讨论】:

    标签: python web-scraping data-mining data-extraction


    【解决方案1】:

    您需要将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"}]}}}
    

    要获取其他位置的数据,可以更改坐标{"lat":"52.51767","lon":"13.40553"}

    【讨论】:

    • 如果您尝试从多个位置抓取数据,我建议您也添加标题。
    猜你喜欢
    • 2021-04-30
    • 1970-01-01
    • 2021-01-10
    • 2015-08-01
    • 2018-09-14
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多