Skip to content

Instantly share code, notes, and snippets.

@mommi84 mommi84/sparql.py
Created Oct 31, 2018

Embed
What would you like to do?
SPARQL Query Execution in Python 2.7
import urllib2, urllib, httplib, json
import sys
MAX_RESULTS = 10000
graph = ""
endpoint = ""
# Execute a SPARQL query.
def sparql_query(query):
param = dict()
param["default-graph-uri"] = graph
param["query"] = query
param["format"] = "JSON"
param["CXML_redir_for_subjs"] = "121"
param["CXML_redir_for_hrefs"] = ""
param["timeout"] = "600000" # ten minutes - works with Virtuoso endpoints
param["debug"] = "on"
try:
resp = urllib2.urlopen(endpoint + "?" + urllib.urlencode(param))
j = resp.read()
resp.close()
except (urllib2.HTTPError, httplib.BadStatusLine):
print("*** Query error. Empty result set. ***")
return []
return json.loads(j)["results"]["bindings"]
# Execute a SPARQL query and iterates over the offsets to get all results.
def sparql_query_all(query):
results = []
for i in xrange(sys.maxint):
offset = i * MAX_RESULTS
part = sparql_query("{} OFFSET {} LIMIT 10000".format(query, offset))
results += part
if len(part) < MAX_RESULTS:
break
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.