Created
October 30, 2016 09:37
-
-
Save moskomule/0ee858c753b5e7e51dd7d45029124574 to your computer and use it in GitHub Desktop.
fetch more than 10000 contents from DBpedia etc. by sparql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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