Skip to content

Instantly share code, notes, and snippets.

@tatiana
Created June 29, 2012 04:29
Show Gist options
  • Save tatiana/3015780 to your computer and use it in GitHub Desktop.
Save tatiana/3015780 to your computer and use it in GitHub Desktop.
SPARQLWrapper example: List who (musicians) have genre Metal
from SPARQLWrapper import SPARQLWrapper, JSON
# List all instances (eg. bands) with genre Metal
query = """
PREFIX db: <http://dbpedia.org/resource/>
PREFIX dbonto: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?who
FROM <http://dbpedia.org>
WHERE {
?who dbonto:genre db:Metal .
}
"""
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
print(result["who"]["value"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment