概要
paiza.ioでpythonやってみた。
練習問題やってみた。
練習問題
dbpediaをsparqlで叩け。
サンプルコード
# coding: utf-8
import json
import urllib.request
url = 'https://ja.dbpedia.org/sparql'
data = {
"query": "SELECT DISTINCT * WHERE {<http://ja.dbpedia.org/resource/東京都> ?p ?o . } LIMIT 10"
}
headers = {
'Accept': 'application/sparql-results+json',
'Content-Type': 'application/x-www-form-urlencoded'
}
req = urllib.request.Request(url=url, data=urllib.parse.urlencode(data).encode('ascii'), headers=headers, method='POST')
with urllib.request.urlopen(req) as res:
body = res.read()
print (body.decode('UTF-8'))
実行結果
{ "head": { "link": [], "vars": ["p", "o"] }, "results": { "distinct": false, "ordered": true, "bindings": [ { "p": { "type": "uri", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" } , "o": { "type": "uri", "value": "http://www.w3.org/2002/07/owl#Thing" }}, { "p": { "type": "uri", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" } , "o": { "type": "uri", "value": "http://www.w3.org/2003/01/geo/wgs84_pos#SpatialThing" }}, { "p": { "type": "uri", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" } , "o": { "type": "uri", "value": "http://dbpedia.org/ontology/AdministrativeRegion" }}, { "p": { "type": "uri", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" } , "o": { "type": "uri", "value": "http://dbpedia.org/ontology/Region" }}, { "p": { "type": "uri", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" } , "o": { "type": "uri", "value": "http://schema.org/AdministrativeArea" }}, { "p": { "type": "uri", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" } , "o": { "type": "uri", "value": "http://www.wikidata.org/entity/Q3455524" }}, { "p": { "type": "uri", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" } , "o": { "type": "uri", "value": "http://schema.org/Place" }}, { "p": { "type": "uri", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" } , "o": { "type": "uri", "value": "http://dbpedia.org/ontology/Location" }}, { "p": { "type": "uri", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" } , "o": { "type": "uri", "value": "http://dbpedia.org/ontology/Place" }}, { "p": { "type": "uri", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" } , "o": { "type": "uri", "value": "http://dbpedia.org/ontology/PopulatedPlace" }} ] } }
成果物
以上。