SparqlWrapper
SparqlWrapper is a Python library that provides a simple interface for querying SPARQL endpoints. SPARQL is a query language for querying data stored in RDF (Resource Description Framework) format. RDF is a standard for modeling data using subject-predicate-object triples. SPARQL allows you to query this data by writing queries in syntax similar to SQL.
A simple example:
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?birthDate
WHERE {
?person dbo:birthDate ?birthDate .
?person foaf:name ?name .
FILTER(lang(?name)='en')
}
LIMIT 10
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
print(result["name"]["value"], result["birthDate"]["value"])
In this example, we are querying the DBpedia endpoint to get the names and birth dates of 10 people. We use the dbo and foaf prefixes to specify the properties we want to query. We are also using the FILTER function to filter out names that are not in English.
The SPARQLWrapper class is used to connect to the SPARQL endpoint. We pass the URL of the endpoint to the constructor. We then set the SPARQL query using the setQuery method and set the return format to JSON using the setReturnFormat method. We then execute the query using the query method and convert the results to a Python dictionary using the convert method.
This is just a simple example to get you started. SparqlWrapper provides many more features that allow you to construct more complex queries and interact with SPARQL endpoints in more advanced ways. I recommend reading through the documentation and trying out some examples to better understand what SparqlWrapper can do.