SPARQL 查询的 python 代码错误
Error in python code with SPARQL query
我正在编写 python 代码来检索 DBpedia 和 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
- 维基数据 SPARQL 端点地址是
http://www.wikidata.org/sparql
,不是 http://www.wikidata.org
。
- 不要删除此优化提示:
hint:Query hint:optimizer "None"
。请参阅 documentation 了解 Blazegraph 优化器提示的工作原理。
- 不要忘记定义前缀(包括
hint:
)。
- 您的 Python 代码中存在小的缩进问题。
- 不要重复问题。
我正在编写 python 代码来检索 DBpedia 和 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
- 维基数据 SPARQL 端点地址是
http://www.wikidata.org/sparql
,不是http://www.wikidata.org
。 - 不要删除此优化提示:
hint:Query hint:optimizer "None"
。请参阅 documentation 了解 Blazegraph 优化器提示的工作原理。 - 不要忘记定义前缀(包括
hint:
)。 - 您的 Python 代码中存在小的缩进问题。
- 不要重复问题。