Skip to content

Instantly share code, notes, and snippets.

@dogrunjp
Last active November 20, 2017 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dogrunjp/67b469a574802e2a5bbb968bf06b3741 to your computer and use it in GitHub Desktop.
Save dogrunjp/67b469a574802e2a5bbb968bf06b3741 to your computer and use it in GitHub Desktop.
PythonのSPARQLWrapperを使ってSPRQL endpointからJSON形式でデータを取得しようとした時、うまくいかないことがあったので、SPARQLWrapperを使わない方法を書いておきます。意外にこちらの方がシンプルかも。
import urllib.request
import urllib.parse
data = urllib.parse.quote('''
PREFIX refexo: <http://purl.jp/bio/01/refexo#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX refexs: <http://refex.dbcls.jp/sample/>
SELECT DISTINCT ?organism
WHERE {
refexs:RES00000003 refexo:organism ?organism .
}
''')
# 細かい設定をするため、Request objectを使う
req = urllib.request.Request("https://integbio.jp/rdf/sparql?query=%s" % data)
req.add_header('Accept', 'application/sparql-results+json')
response = urllib.request.urlopen(req)
result = response.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment