pythonjsonwikidatawikidata-apiwikidata-query-service

Get itemlabel in python from Wikidata API


I am trying to create a list with all Harry Potter characters names using the Wikidata API. I want to get itemlabels (character names) from the link below into my Python notebook.

Here is the Wikidata Query Service query which runs as I want.

import requests
import json
hpCharURL = "https://query.wikidata.org/sparql?query= SELECT DISTINCT 
?item ?itemLabel WHERE { {?item wdt:P31 ?sub1 . 
?sub1 (wdt:P279|wdt:P131)* wd:Q95074 . 
?item wdt:P1080 ?sub2 . 
?sub2 (wdt:P279|wdt:P131)* wd:Q5410773 } 
SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' . }} 
&format = JSON"
r2 = requests.get(hpCharURL)
r2.json()

I keep getting this error after running the last line of code above:

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The results are still coming back in XML and not JSON, even though I specify JSON at the end of the query. Any ideas on how to fix this would be appreciated.


Solution

  • Yes, you are still getting xml.

    To request a JSON response, add a header to your request, like so:

    headers = {"Accept" : "application/json"}
    r2 = requests.get(hpCharURL, headers=headers)
    r2.json()