3

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.

logi-kal
7,8996 gold badges35 silver badges48 bronze badges
asked Mar 14, 2016 at 16:58
1

2 Answers 2

3

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()
answered Mar 15, 2016 at 20:05
Sign up to request clarification or add additional context in comments.

3 Comments

I changed the second line to below and it worked! r2 = requests.get(hpCharURL, headers=headers)
Fixed an issue with my answer
Oh ok good you caught my mistake. Please accept the answer if the issue is resolved.
0

There is a handy package to fetch data from wikipeidia. Check it https://pypi.python.org/pypi/wikipedia

answered Mar 15, 2016 at 20:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.