hi every one
i have a query in python, which requests an sparql query to dbpedia.
i want to get triples with special object.
here is my query:
query='''
PREFIX rs: <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
select distinct ?sub,?predicate
where
{{
?sub ?predicate rs:{a1}.
}}
'''.format(a1=ent)
url = 'https://dbpedia.org/sparql/'
myobj = {"query":query, "format":"json", "default-graph-uri":"http://dbpedia.org"}
try:
r=requests.get(url, params=myobj)
except Exception as ex:
print("error for question for entity:"+ent+" : ",ex)
but the problem is when my object has special characters, i get 400 from dbpedia.
here is some examples that had bad request.
error for question for entity:Off_Limits_(1988_film) : Expecting value: line 1 column 1 (char 0)
error for question for entity:Faith_of_My_Fathers_(film) : Expecting value: line 1 column 1 (char 0)
error for question for entity:A_Rumor_of_War_(miniseries) : Expecting value: line 1 column 1 (char 0)
error for question for entity:Bat*21 : Expecting value: line 1 column 1 (char 0)
error for question for entity:Hearts_and_Minds_(film) : Expecting value: line 1 column 1 (char 0)
error for question for entity:The_Green_Berets_(film) : Expecting value: line 1 column 1 (char 0)
error for question for entity:War_in_Vietnam_(1954–59) : Expecting value: line 1 column 1 (char 0)
error for question for entity:Alice's_Restaurant_(film) : Expecting value: line 1 column 1 (char 0)
error for question for entity:Sir!_No_Sir! : Expecting value: line 1 column 1 (char 0)
error for question for entity:Jacob's_Ladder_(film) : Expecting value: line 1 column 1 (char 0)
error for question for entity:List_of_allied_military_operations_of_the_Vietnam_War_(1973–74) : Expecting value: line 1 column 1 (char 0)
error for question for entity:War_in_Vietnam_(1945–46) : Expecting value: line 1 column 1 (char 0)
error for question for entity:The_War_at_Home_(film) : Expecting value: line 1 column 1 (char 0)
error for question for entity:Missing_in_Action_(film) : Expecting value: line 1 column 1 (char 0)
<Response [400]>
error for question for entity:Off_Limits_(1988_film) : Expecting value: line 1 column 1 (char 0)
so how can i request to dbpedia in python correctly?
You may also find rdflib useful for working with SPARQL queries in Python. Using it to pre-bind variables in a query can help in getting the syntax details correct and it will also help avoid potential problems with SPARQL injection .
Here is an example:
import rdflib
ent = "Off_Limits_(1988_film)"
obj = rdflib.URIRef(ent, base="http://dbpedia.org/resource/")
q = """
SELECT DISTINCT ?sub ?pred
WHERE {
SERVICE <http://dbpedia.org/sparql> {
?sub ?pred ?obj .
}
}
"""
for row in rdflib.Graph().query(q, initBindings={"obj": obj}):
print(str(row.sub), str(row.pred))
See also prepared queries in the rdflib documentation.
Special characters in prefixed names in SPARQL need to be escaped by prepending a backslash character. See this for the allowed form of SPARQL local names.
So for example instead of rs:Off_Limits_(1988_film)
you need to use rs:Off_Limits_\(1988_film\)
in the query.
See this for an example query.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.