Python SPARQL client example

Originally published August 5, 2013

I wrote about using linked data sources like DBPedia yesterday but I should have included some example code for you to play with. I will get you started with these directions:

Start by installing three packages:

sudo easy_install simplejson
sudo easy_install RDFLib
sudo easy_install SPARQLWrapper
If you use the faceted search browser for DBPedia and search for "Berlin" and see a likely URI dbpedia:Berlin_Berlin to start with. The following code finds all triples that have dbpedia:Berlin_Berlin as an object and displays the subjects and predicates. Then, for all of the subjects in these triples, I search again for all triples with these subjects:
from SPARQLWrapper import SPARQLWrapper, JSON

sparql = SPARQLWrapper("")
    PREFIX rdfs: <>
    PREFIX dbpedia: <>
    SELECT ?s ?p
    WHERE { ?s ?p dbpedia:Berlin_Berlin } LIMIT 5
results = sparql.query().convert()

print("subject and predicate of all triples with object == dbpedia:Berlin_Berlin")

for result in results["results"]["bindings"]:
    print("s=" + result["s"]["value"] + "\tp=" + result["p"]["value"])

print("loop over all subjects returned in first query:")
for result in results["results"]["bindings"]:
        PREFIX rdfs: <>
        PREFIX dbpedia: <>
        SELECT ?p ?o
        WHERE { <""" + result["s"]["value"] +
          """> ?p ?o } LIMIT 10""")
    results2 = sparql.query().convert()
    for result2 in results2["results"]["bindings"]:
        print("p=" + result2["p"]["value"] + "\to=" + result2["o"]["value"])
This is really a simple (and contrived) example that you can experiment with in just a few minutes, but it shows the mechanics of "spidering" linked data. You will need to learn the SPARQL query language for any real application. You can find many good SPARQL tutorials on the web, or you can grab a free PDF of either the Java or Common Lisp edition of my book "Practical Semantic Web and Linked Data Applications" on my my books web page.

I like to use DBPedia's snorql web app to experiment with SPARQL queries, get the queries working, then implement them in Python or Ruby scripts.


Popular posts from this blog

Custom built SBCL and using spaCy and TensorFlow in Common Lisp

I have tried to take advantage of extra time during the COVID-19 pandemic

GANs and other deep learning models for cooking recipes