Created
December 11, 2017 10:39
-
-
Save erickguan/d814edf239717fc3ebef1756943b6a76 to your computer and use it in GitHub Desktop.
Simple SPARQL query on DBpedia and Wikidata
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
[[source]] | |
url = "https://pypi.python.org/simple" | |
verify_ssl = true | |
name = "pypi" | |
[dev-packages] | |
ipykernel = "*" | |
[packages] | |
rdflib = "*" | |
imdbpy = "*" | |
sparqlwrapper = "*" | |
[requires] | |
python_version = "3.6" |
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
# -*- coding: utf-8 -*- | |
from SPARQLWrapper import SPARQLWrapper, JSON | |
sparql = SPARQLWrapper("http://dbpedia.org/sparql") | |
sparql.setQuery(""" | |
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | |
SELECT ?label | |
WHERE { <http://dbpedia.org/resource/Asturias> rdfs:label ?label } | |
""") | |
sparql.setReturnFormat(JSON) | |
results = sparql.query() | |
results.print_results() | |
sparql.setQuery(""" | |
PREFIX wd: <http://www.wikidata.org/entity/> | |
PREFIX wds: <http://www.wikidata.org/entity/statement/> | |
PREFIX wdv: <http://www.wikidata.org/value/> | |
PREFIX wdt: <http://www.wikidata.org/prop/direct/> | |
PREFIX wikibase: <http://wikiba.se/ontology#> | |
PREFIX p: <http://www.wikidata.org/prop/> | |
PREFIX ps: <http://www.wikidata.org/prop/statement/> | |
PREFIX pq: <http://www.wikidata.org/prop/qualifier/> | |
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | |
PREFIX bd: <http://www.bigdata.com/rdf#> | |
SELECT ?label | |
WHERE { | |
wd:Q3934 rdfs:label ?label | |
} | |
""") | |
sparql.setReturnFormat(JSON) | |
results = sparql.query() | |
results.print_results() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment