SPARQL Query on Python: Virtuoso 37000 Error SP030

344 views Asked by At

I have a problem building a python script to run a query in SPARQL.

What I would like to get is the list of information specified immediately after the SELECT (so artist, discography, etc..), for each artist possible. The script is as follows:

from SPARQLWrapper import SPARQLWrapper, JSON

sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""
    PREFIX wdt: <http://www.wikidata.org/prop/direct/
     SELECT ?artist ?discography ?artistLabel ?genreLabel ?yearstartLabel 
?languageLabel WHERE {
  ?artist wdt:P106 wd:Q488205;
  ?artist wdt:P358 ?discography;
  ?artist wdt:P136 ?genre;
  ?artist wdt:P18 ?image;
  ?artist wdt:P2031 ?yearstart;
  ?artist wdt:P1412 ?language.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["label"]["value"])

When I try to run the code, the compiler provides me with this error:

SPARQLWrapper.SPARQLExceptions.QueryBadFormed: QueryBadFormed: a bad 
request has been sent to the endpoint, probably the sparql query is bad 
formed. 

Response:
 b'Virtuoso 37000 Error SP030: SPARQL compiler, line 3: Missing 
 <namespace-iri-string> in PREFIX declaration at \'<\' before 
 \'http:\'\n\nSPARQL query:\n#output-format:application/sparql- 
results+json\n\n    PREFIX wdt: <http://www.wikidata.org/prop/direct/\n     
 SELECT ?artist ?discography ?artistLabel ?genreLabel ?yearstartLabel ? 
languageLabel WHERE {\n  ?artist wdt:P106 wd:Q488205;\n  ?artist wdt:P358 
?discography;\n  ?artist wdt:P136 ?genre;\n  ?artist wdt:P18 ?image;\n  ? 
artist wdt:P2031 ?yearstart;\n  ?artist wdt:P1412 ?language.\n  SERVICE 
wikibase:label { bd:serviceParam wikibase:language "en". }\n}\n'

As I explained earlier, what I would like would be the list of artists with the relevant information sought.

P.S. The query I'm trying to run provides a positive response if tested on https://query.wikidata.org/, but instead by error if I try it on https://dbpedia.org/sparql.

I thank you in advance for your answer, greetings to everyone.

0

There are 0 answers