来自BNCF endpoin的Sparql查询JSON错误

2024-05-16 01:05:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从位于this endpoint的BNCF检索结果。在

我的查询(以“ab”为例)是:

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?source ?label ?content
                WHERE {
                    ?source a skos:Concept;
                        skos:prefLabel ?label; 
                        skos:scopeNote ?content.
                FILTER regex(str(?label), "ab", "i")
            }

事实上,如果您尝试运行查询,它是正确的。 但当我试图从python中获取结果时,出现了一个错误:

^{pr2}$

这是我的python代码:

__3store = "http://digitale.bncf.firenze.sbn.it/openrdf-workbench/repositories/NS_03_2014/query"
sparql = SPARQLUpdateStore(queryEndpoint=__3store)
sparql.setReturnFormat(JSON)
results = sparql.query(query_rdf).convert()
print json.dumps(result, separators=(',',':'))

在我的代码是这样之前,我根据this answer尝试了上面的代码:

__3store = "http://digitale.bncf.firenze.sbn.it/openrdf-workbench/repositories/NS_03_2014/query"
sparql = SPARQLWrapper(__3store,returnFormat="json")
sparql.setQuery(query_rdf)
result = sparql.query().convert() 
print json.dumps(result, separators=(',',':'))

但两者都犯了同样的错误。在

有人知道怎么修理吗? 谢谢

编辑:

这是python代码,希望足够理解

import sys
sys.path.append ('cgi/lib')
import rdflib
from rdflib.plugins.stores.sparqlstore import SPARQLUpdateStore, SPARQLStore
import json
from SPARQLWrapper import SPARQLWrapper, JSON

#MAIN
print "Content-type: application/json"
print
prefix_SKOS =       "prefix skos:      <http://www.w3.org/2004/02/skos/core#>"
crlf = "\n"
query_rdf = ""
query_rdf += prefix_SKOS + crlf
query_rdf += '''
            SELECT DISTINCT ?source ?title ?content
                WHERE {
                    ?source a skos:Concept;
                        skos:prefLabel ?title; 
                        skos:scopeNote ?content.
                FILTER regex(str(?title), "ab", "i")
            }

        '''
__3store = "http://digitale.bncf.firenze.sbn.it/openrdf-workbench/repositories/NS_03_2014/query"
sparql = SPARQLWrapper(__3store,returnFormat="json")
sparql.setQuery(query_rdf)
result = sparql.query().convert() 

print result

在Python shell中运行此命令将返回:

Content-type: application/json


Warning (from warnings module):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SPARQLWrapper-1.6.4-py2.7.egg/SPARQLWrapper/Wrapper.py", line 689
RuntimeWarning: Format requested was JSON, but XML (application/sparql-results+xml;charset=UTF-8) has been returned by the endpoint
<xml.dom.minidom.Document instance at 0x105add710>

所以我认为如果我指定Json作为返回格式,结果也总是XML。在


Tags: 代码importjsonhttpsourceabrdfskos
1条回答
网友
1楼 · 发布于 2024-05-16 01:05:22

这里有几个问题:

首先,如果您想通过rdflib的图形接口访问SPARQL存储(例如,您可以添加三元组,可以迭代它们,等等),那么应该只使用rdflib中的SPARQLUpdateStore。 如果您想自己编写SPARQL查询,应该使用SPARQLWrapper。在

第二,如果您要求SPARQLWrapper返回JSON,那么它实际上会向服务器请求两种mime类型,这些类型是我们所称的“JSON”最常见和标准化的类型,如herehere

_SPARQL_JSON = ["application/sparql-results+json", "text/javascript", "application/json"]

似乎您的服务器确实理解application/sparql-results+json,但不是一个组合的“give me any these mime types header”,因为rdflib编译它以实现最大的互操作性(因此您的服务器基本上不完全支持HTTP Accept Headers):

^{pr2}$

将返回:

HTTP/1.1 200 OK
Date: Mon, 18 May 2015 13:13:45 GMT
Server: Apache/2.2.17 (Unix) PHP/5.3.6 mod_jk/1.2.31
...
Content-Type: application/sparql-results+json;charset=UTF-8

{
  "head" : {
    "vars" : [ ],
    "vars" : [ "source", "label", "content" ],
    "link" : [ "info" ]
  },
  "results" : {
    "bindings" : [ {
      "content" : {
        "type" : "literal",
        "value" : "Il lasciare ingiustificatamente qualcuno o qualcosa di cui si è responsabili"
      },
      "source" : {
        "type" : "uri",
        "value" : "http://purl.org/bncf/tid/12445"
      },
      "label" : {
        "xml:lang" : "it",
        "type" : "literal",
        "value" : "Abbandono"
      }
    },
...

所以一切正常,但如果我们要求组合的、更具互操作性的mime类型:

curl -i -G -H 'Accept: application/sparql-results+json,text/javascript,application/json'  data-urlencode 'query=PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?source ?label ?content
WHERE {
 ?source a skos:Concept;
 skos:prefLabel ?label;
 skos:scopeNote ?content.
 FILTER regex(str(?label), "ab", "i")
}' http://digitale.bncf.firenze.sbn.it/openrdf-workbench/repositories/NS_03_2014/query

我们得到一个xml结果:

HTTP/1.1 200 OK
Server: Apache/2.2.17 (Unix) PHP/5.3.6 mod_jk/1.2.31
...
Content-Type: application/sparql-results+xml;charset=UTF-8

<?xml version='1.0' encoding='UTF-8'?>
...

长话短说:这是你使用的服务器中的一个错误。下面是一个糟糕的解决方法(SPARQLWrapper似乎不仅允许我们手动设置头,还可以在_createRequest中无条件地覆盖它们),但是它可以工作:

In [1]: import SPARQLWrapper as sw

In [2]: sparql = sw.SPARQLWrapper("http://digitale.bncf.firenze.sbn.it/openrdf-workbench/repositories/NS_03_2014/query")

In [3]: sparql.setReturnFormat(sw.JSON)

In [4]: sparql.setQuery('''                                                                                                     PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?source ?label ?content
                WHERE {
                    ?source a skos:Concept;
                        skos:prefLabel ?label;
                        skos:scopeNote ?content.
                FILTER regex(str(?label), "ab", "i")
            }
''')

In [5]: request = sparql._createRequest()

In [6]: request.add_header('Accept', 'application/sparql-results+json')

In [7]: from urllib2 import urlopen

In [8]: response = urlopen(request)

In [9]: res = sw.Wrapper.QueryResult((response, sparql.returnFormat))

In [10]: result = res.convert()

In [11]: result
Out[11]:
{u'head': {u'link': [u'info'], u'vars': [u'source', u'label', u'content']},
 u'results': {u'bindings': [{u'content': {u'type': u'literal',
     u'value': u'Il lasciare ingiustificatamente qualcuno o qualcosa di cui si \xe8 responsabili'},
    u'label': {u'type': u'literal',
     u'value': u'Abbandono',
     u'xml:lang': u'it'},
    u'source': {u'type': u'uri', u'value': u'http://purl.org/bncf/tid/12445'}},
   ...

相关问题 更多 >