Michel
Michel

Reputation: 66

Python SPARQLWrapper: specifying URIs doesn't work

I use the following Python code to retrieve all triples from my sparql endpoint:

from SPARQLWrapper import SPARQLWrapper, JSON
import sys

def get_results(endpoint_url, query):
    user_agent = "WDQS-example Python/%s.%s" % (sys.version_info[0], sys.version_info[1])
    sparql = SPARQLWrapper(endpoint_url, agent=user_agent)
    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    return sparql.query().convert()

endpoint='https://music.wikibase.cloud/query/sparql'
query="""construct where {?s ?p ?o.}"""
get_results(endpoint,query)

This works, it gives me all triples, and I see one of them has the predicate https://music.wikibase.cloud/prop/direct/P2. Now I want to list all triples with that predicate, so I execute:

query="""construct where {?s <https://music.wikibase.cloud/prop/direct/P2> ?o.}"""
get_results(endpoint,query)

Yielding no results, but we have seen in the first query that there are triples with that predicate.

Using the sparql web interface at https://music.wikibase.cloud/query, the second query works as expected. What am I doing wrong in the Python code?

Upvotes: 2

Views: 76

Answers (0)

Related Questions