我在https://github.com/RDFLib/sparqlwrapper/blob/master/scripts/example.py找到了示例代码
我稍微修改了一下
from SPARQLWrapper import SPARQLWrapper, JSON, XML, N3, RDF, CSV, TSV
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""
SELECT ?subject ?predicate ?object
FROM <http://graph.something.com>
FROM <http://ontology.something.com>
WHERE {
?subject ?predicate ?object
} LIMIT 10
""")
# JSON example
print '\n\n*** JSON Example'
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
print result["label"]["value"]
大部分都不起作用,但是当我指向 dbpedia.org 站点时,我需要的部分确实起作用。但是,当我将它定向到我自己的网站(除了我之外的任何人都无法使用)时,我收到如下错误:
*** JSON Example Traceback (most recent call last): File "test01.py", line 29, in <module>
results = sparql.query().convert() File "/usr/local/lib/python2.7/dist-packages/SPARQLWrapper/Wrapper.py", line 798, in query
return QueryResult(self._query()) File "/usr/local/lib/python2.7/dist-packages/SPARQLWrapper/Wrapper.py", line 766, in _query
response = urlopener(request) File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout) File "/usr/lib/python2.7/urllib2.py", line 429, in open
response = self._open(req, data) File "/usr/lib/python2.7/urllib2.py", line 447, in _open
'_open', req) File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 1228, in http_open
return self.do_open(httplib.HTTPConnection, req) File "/usr/lib/python2.7/urllib2.py", line 1198, in do_open
raise URLError(err) urllib2.URLError: <urlopen error [Errno 111] Connection refused>
知道发生了什么,更重要的是如何解决它?