pythondbpediawikidatasparqlwrapperfederated-queries

Error in python code with SPARQL query


I'm writing a python code to retrieve all actors which is common to both DBpedia and Wikidata. And also getting some additional information like awards received from wikidata. But its throwing an error. I'm not sure how to correct this error. Here is my python code:

from SPARQLWrapper import SPARQLWrapper, JSON

sparql = SPARQLWrapper("https://query.wikidata.org/")
sparql.setQuery("""
PREFIX  dbp:  <http://dbpedia.org/property/>
PREFIX  movie: <http://data.linkedmdb.org/resource/movie/>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT DISTINCT ?Actor ?award_received
WHERE {
SERVICE <http://dbpedia.org/sparql> {
?c rdf:type <http://umbel.org/umbel/rc/Actor> . 
?c rdfs:label ?Actor.
FILTER (LANG(?Actor)="en").
?c owl:sameAs ?wikidata_actor .
   FILTER (STRSTARTS(STR(?wikidata_actor), "http://www.wikidata.org"))}
?wikidata_actor wdt:P166 ?award_received.
}
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
if ("Actor" in result):
    print(result["Actor"]["value"])
else:
    url = 'NONE'

if ("award_received" in result):
    print(result["award_received"]["value"])
else:
    url = 'NONE'

Here is the error I'm getting:

/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 
"/Users/ashwinis/PycharmProjects/semantic web/club.py"
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/SPARQLWrapper/Wrapper.py:762: RuntimeWarning: unknown response 
content type, returning raw response...
warnings.warn("unknown response content type, returning raw 
response...", RuntimeWarning)
Traceback (most recent call last):
File "/Users/ashwinis/PycharmProjects/semantic web/club.py", line 27, 
in <module>
for result in results["results"]["bindings"]:
TypeError: string indices must be integers, not str

Process finished with exit code 1

Solution

    1. Wikidata SPARQL endpoint address is http://www.wikidata.org/sparql, not http://www.wikidata.org.
    2. Do not remove this optimization hint: hint:Query hint:optimizer "None". See documentation on how Blazegraph optimizer hints work.
    3. Do not forget to define prefixes (including hint:).
    4. There are minor indentation problems in your Python code.
    5. Do not duplicate questions.