# filename: ex363.py # Query Linked Movie database endpoint about common actors of # two directors and output HTML page with links to Freebase. from SPARQLWrapper import SPARQLWrapper, JSON director1 = "Steven Spielberg" director2 = "Stanley Kubrick" sparql = SPARQLWrapper("http://data.linkedmdb.org/sparql") queryString = """ PREFIX m: PREFIX foaf: SELECT DISTINCT ?actorName ?freebaseURI WHERE { ?dir1 m:director_name "DIR1-NAME" . ?dir2 m:director_name "DIR2-NAME" . ?dir1film m:director ?dir1 ; m:actor ?actor . ?dir2film m:director ?dir2 ; m:actor ?actor . ?actor m:actor_name ?actorName ; foaf:page ?freebaseURI . } """ queryString = queryString.replace("DIR1-NAME",director1) queryString = queryString.replace("DIR2-NAME",director2) sparql.setQuery(queryString) sparql.setReturnFormat(JSON) results = sparql.query().convert() print """ results """ print "

Actors directed by both " + director1 + " and " + director2 + "

" if (len(results["results"]["bindings"]) == 0): print "

No results found.

" else: for result in results["results"]["bindings"]: actorName = result["actorName"]["value"] freebaseURI = result["freebaseURI"]["value"] print "

" + actorName + "

" print ""