0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paiza.ioでpython その6

Posted at

概要

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" }} ] } }

成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?