2

我正在编写一个 python 代码来检索 DBpedia 和 Wikidata 共有的所有参与者。并且还获得了一些额外的信息,例如从 wikidata 获得的奖励。但它抛出一个错误。我不确定如何纠正这个错误。这是我的python代码:

from SPARQLWrapper import SPARQLWrapper, JSON

sparql = SPARQLWrapper("https://query.wikidata.org/")
sparql.setQuery("""
PREFIX  dbp:  <http://dbpedia.org/property/>
PREFIX  movie: <http://data.linkedmdb.org/resource/movie/>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT DISTINCT ?Actor ?award_received
WHERE {
SERVICE <http://dbpedia.org/sparql> {
?c rdf:type <http://umbel.org/umbel/rc/Actor> . 
?c rdfs:label ?Actor.
FILTER (LANG(?Actor)="en").
?c owl:sameAs ?wikidata_actor .
   FILTER (STRSTARTS(STR(?wikidata_actor), "http://www.wikidata.org"))}
?wikidata_actor wdt:P166 ?award_received.
}
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
if ("Actor" in result):
    print(result["Actor"]["value"])
else:
    url = 'NONE'

if ("award_received" in result):
    print(result["award_received"]["value"])
else:
    url = 'NONE'

这是我得到的错误:

/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 
"/Users/ashwinis/PycharmProjects/semantic web/club.py"
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/SPARQLWrapper/Wrapper.py:762: RuntimeWarning: unknown response 
content type, returning raw response...
warnings.warn("unknown response content type, returning raw 
response...", RuntimeWarning)
Traceback (most recent call last):
File "/Users/ashwinis/PycharmProjects/semantic web/club.py", line 27, 
in <module>
for result in results["results"]["bindings"]:
TypeError: string indices must be integers, not str

Process finished with exit code 1
4

1 回答 1

1
  1. Wikidata SPARQL 端点地址是 http://www.wikidata.org/sparql,不是http://www.wikidata.org
  2. 不要删除此优化提示:hint:Query hint:optimizer "None". 请参阅有关 Blazegraph 优化器提示如何工作的文档。
  3. 不要忘记定义前缀(包括hint:)。
  4. 您的 Python 代码中存在轻微的缩进问题。
  5. 不要重复问题。
于 2017-05-20T22:16:23.640 回答