Search code examples
pythonweb-scrapingdata-miningdata-extraction

How to send a post requests to Graphql in python


I want to scrape this website https://www.weisse-liste-pflege.de/ if we search like berlin in the search bar and open the developer tool and in the network tab a post request is made to Graphql and it contains all the data in JSON and I want that data. here is my code.

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

any please guide me how I can achieve json data


Solution

  • You need to pass the payload seen in Request Payload section as json parameter in your request.

    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"}