2

我尝试了一个小的 python 脚本来测试我的 SPARQL 请求。然而,仅仅下一个简单的代码是行不通的。

from SPARQLWrapper import SPARQLWrapper, JSON
import rdflib
#connect to the sparql point
sparql = SPARQLWrapper("http://localhost:3030/sparql")
#SPARQL request
sparql.setQuery("""
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rme: <http://www.semanticweb.org/reminer/>

SELECT ?o
WHERE { ?s ?p ?o }
LIMIT 1
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
     print(result["o"]["value"])

我的代码在转换步骤冻结了很长时间,然后给了我一个 URLError。当我停止脚本时,请参阅下一条消息:

HTTPError                                 Traceback (most recent call last)
<ipython-input-6-2ab63307a418> in <module>()
     18 """)
     19 sparql.setReturnFormat(JSON)
---> 20 results = sparql.query().convert()
     21 
     22 for result in results["results"]["bindings"]:

/Users/francocy/anaconda/lib/python3.4/site-packages/SPARQLWrapper/Wrapper.py in query(self)
    533             @rtype: L{QueryResult} instance
    534         """
--> 535         return QueryResult(self._query())
    536 
    537     def queryAndConvert(self):

/Users/francocy/anaconda/lib/python3.4/site-packages/SPARQLWrapper/Wrapper.py in _query(self)
    513                 raise EndPointInternalError(e.read())
    514             else:
--> 515                 raise e
    516 
    517     def query(self):

/Users/francocy/anaconda/lib/python3.4/site-packages/SPARQLWrapper/Wrapper.py in _query(self)
    503 
    504         try:
--> 505             response = urlopener(request)
    506             return response, self.returnFormat
    507         except urllib.error.HTTPError as e:

/Users/francocy/anaconda/lib/python3.4/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    159     else:
    160         opener = _opener
--> 161     return opener.open(url, data, timeout)
    162 
    163 def install_opener(opener):

/Users/francocy/anaconda/lib/python3.4/urllib/request.py in open(self, fullurl, data, timeout)
    467         for processor in self.process_response.get(protocol, []):
    468             meth = getattr(processor, meth_name)
--> 469             response = meth(req, response)
    470 
    471         return response

/Users/francocy/anaconda/lib/python3.4/urllib/request.py in http_response(self, request, response)
    577         if not (200 <= code < 300):
    578             response = self.parent.error(
--> 579                 'http', request, response, code, msg, hdrs)
    580 
    581         return response

/Users/francocy/anaconda/lib/python3.4/urllib/request.py in error(self, proto, *args)
    505         if http_err:
    506             args = (dict, 'default', 'http_error_default') + orig_args
--> 507             return self._call_chain(*args)
    508 
    509 # XXX probably also want an abstract factory that knows when it makes

/Users/francocy/anaconda/lib/python3.4/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    439         for handler in handlers:
    440             func = getattr(handler, meth_name)
--> 441             result = func(*args)
    442             if result is not None:
    443                 return result

/Users/francocy/anaconda/lib/python3.4/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
    585 class HTTPDefaultErrorHandler(BaseHandler):
    586     def http_error_default(self, req, fp, code, msg, hdrs):
--> 587         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    588 
    589 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 403: Forbidden

我对 2.7 和 3.4 的情况相同。编辑:我将连接从 Wifi 更改为 Intranet。我的脚本适用于 DBpedia Sparql 端点,但是当我在本地服务器上请求时出现 Http 错误。这似乎是代理或访问我的本地服务器的问题。

在此先感谢您的帮助。

4

3 回答 3

3

对于 2019 年来到这里的用户,对于访问 Wikidata sparql 端点的错误,Wikidata 强制执行严格的用户代理策略,请参阅此存档(感谢 Pere)Wikidata 项目聊天,其中表示应用程序发送信息标头表明行为良好的非bot 脚本,另见Wikimedia 的用户代理策略

根据文档agent,我们可以使用实例变量设置用户代理。用户代理 HTTP 标头在MDN Web 文档中进行了描述。最后,您可以将类对象初始化为,

sparql = SPARQLWrapper("https://query.wikidata.org/sparql", agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11")

希望这可以帮助!

于 2019-07-04T13:02:36.450 回答
1

如果您尝试通过 python 脚本在本地 fuseki 服务器上执行一些 SPARQL 请求,您可能会受到一些代理问题的干扰。要解决此问题,您可以使用 urllib 的自动检测属性。

from SPARQLWrapper import SPARQLWrapper, JSON, XML
#import urllib.request module. Don't forget for Python 3.4 the urllib has been split into several different modules.
import urllib.request

#if the arg is empty in ProxyHandler, urllib will find itself your proxy config.
proxy_support = urllib.request.ProxyHandler({})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)

#connect to the sparql point
sparql = SPARQLWrapper("http://localhost:3030/yourOwnDb/sparql")
#SPARQL request
sparql.setQuery("""
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX owl: <http://www.w3.org/2002/07/owl#>
    PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

    SELECT ?o ?p
    WHERE { ?s ?p ?o }
    LIMIT 1
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["o"]["value"])
于 2015-06-10T15:09:20.913 回答
0

正如错误告诉你的那样:

操作超时

似乎当您运行代码时,您的连接无法访问 dbpedia.org。

现在运行您的代码,它会立即为我返回以下内容:

http://www.openlinksw.com/schemas/virtrdf#QuadMapFormat

所以在生产中你可能想抓住它URLError并以某种方式处理它。


问题编辑后更新:

目前SPARQLWrapper依赖于urllib2执行它的请求,所以如果你在一个代理后面,你应该可以使用urllib2's ProxyHandlerlike here

proxy = urllib2.ProxyHandler({'http': '192.168.x.x'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
# and then:
results = sparql.query().convert()
于 2015-06-10T13:38:08.160 回答