Search code examples
pythonjsonrequesturllib

Parameters are ignored in python web request for JSON data


I try to read JSON-formatted data from the following public URL: http://ws-old.parlament.ch/factions?format=json. Unfortunately, I was not able to convert the response to JSON as I always get the HTML-formatted content back from my request. Somehow the request seems to completely ignore the parameters for JSON formatting passed with the URL:

import urllib.request
response = urllib.request.urlopen('http://ws-old.parlament.ch/factions?format=json')
response_text = response.read()
print(response_text) #why is this HTML?

Does somebody know how I am able to get the JSON formatted content as displayed in the web browser?


Solution

  • You need to add "Accept": "text/json" to request header.

    For example using requests package:

    r = requests.get(r'http://ws-old.parlament.ch/factions?format=json',
                     headers={'Accept':'text/json'})
    print(r.json())
    

    Result:

    [{'id': 3, 'updated': '2022-02-22T14:59:17Z', 'abbreviation': ...