Skip to content

Instantly share code, notes, and snippets.

@moskomule
Created October 30, 2016 09:37
Show Gist options
  • Save moskomule/0ee858c753b5e7e51dd7d45029124574 to your computer and use it in GitHub Desktop.
Save moskomule/0ee858c753b5e7e51dd7d45029124574 to your computer and use it in GitHub Desktop.
fetch more than 10000 contents from DBpedia etc. by sparql
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("http://localhost:8890/sparql")
for i in range(25):
query = """
select ?slabel ?olabel
where {
?s rdfs:subClassOf ?o.
?s rdf:type owl:Class.
?o rdf:type owl:Class.
optional {?s rdfs:label ?slabel.}
optional {?o rdfs:label ?olabel.}
}
LIMIT 1000
OFFSET """ + str(i) + "000"
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
l = []
for re in results["results"]["bindings"]:
l.append("{} {}".format(re["olabel"]["value"], re["slabel"]["value"]))
with open("result.tsv","a") as f:
for x in l:
f.write(x + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment