我正在尝试接收标签包含特定字符串的所有属性。我使用以下查询:
SELECT ?p ?l count(?p) as ?count WHERE {
?someobj ?p ?s .
?p a <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
?p <http://www.w3.org/2000/01/rdf-schema#label> ?l .
?l bif:contains "string" .
FILTER (lang(?l) = 'en').
FILTER (!isLiteral(?someobj)).
} ORDER BY DESC(?count) LIMIT 5
通过公共(public) DBPedia 端点 @ http://dbpedia.org/sparql 发出查询时,它有效,并返回我想要的内容。但是,当我在 Python 脚本中通过 SPARQLWrapper 执行相同操作时,我不断收到:
File "E:\thesis\sem_web21.py", line 254, in findWord
results = sparql.query().convert()
File "build/bdist.linux-i686/egg/SPARQLWrapper/Wrapper.py", line 355, in query
return QueryResult(self._query())
File "build/bdist.linux-i686/egg/SPARQLWrapper/Wrapper.py", line 334, in _query
raise e
HTTPError: HTTP Error 500: SPARQL Request Failed
我尝试过查询的变体,有或没有计数和排序,有或没有限制。我不断收到 HTTP 500。我不认为这是端点不稳定的问题,因为我对同一脚本中的其他查询没有问题,它只会因该查询而停止。
用于检索对象的类似查询工作正常(都在公共(public)端点通过我的脚本):
SELECT ?s ?l count(?s) as ?count WHERE {
?someobj ?p ?s .
?s <http://www.w3.org/2000/01/rdf-schema#label> ?l .
?l bif:contains "computer" .
FILTER (!regex(str(?s), '^http://dbpedia.org/resource/Category:')).
FILTER (!regex(str(?s), '^http://dbpedia.org/resource/List')).
FILTER (!regex(str(?s), '^http://sw.opencyc.org/')).
FILTER (lang(?l) = 'en').
FILTER (!isLiteral(?someobj)).
} ORDER BY DESC(?count) LIMIT 20
知道是什么原因造成的吗?或者知道如何检索更具体的错误?提前致谢。
最佳答案
我认为这是 dbpedia 方面的超时错误,因为它在不同的图表中查找它。当您通过 dbpedia Web 界面尝试它时,它始终包含您正在查询的图表的 uri。因此,请尝试将其添加到您的查询中:
SELECT ?p ?l count(?p) as ?count FROM <http://dbpedia.org> WHERE {
?someobj ?p ?s .
?p a <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
?p <http://www.w3.org/2000/01/rdf-schema#label> ?l .
?l bif:contains "string" .
FILTER (lang(?l) = 'en').
FILTER (!isLiteral(?someobj)).
} ORDER BY DESC(?count) LIMIT 5
然后再试一次。
使用以下 python 脚本进行尝试:
import sys
import urllib,urllib2
def query_e(query,epr,soft_limit=True):
try:
params = urllib.urlencode({'query': query})
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request(epr+'?'+params)
request.add_header('Accept', 'application/json')
request.get_method = lambda: 'GET'
url = opener.open(request)
data = url.read()
return data
except Exception, e:
traceback.print_exc(file=sys.stdout)
raise e
关于python - 使用 Python 的 SPARQLwrapper 通过 SPARQL 查询 DBPedia 端点获取 HTTP 错误 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7795689/