使用Python处理时sparql查询中的错误

时间:2018-02-22 16:12:58

标签: python sparql

我正在使用Python处理sparql查询。

def query(var):
query_with_placeholder="""
SELECT ?same_context as ?context  str(?context_label) as ?context_label ?same_s as ?s str(?s_label) as ?s_label ?s_type ?p "$ENTITY_CODE$" as ?gene  str(?text) as ?text 
WHERE {
?transcript <http://purl.obolibrary.org/obo/SO_transcribed_from> <http://rdf.ebi.ac.uk/resource/ensembl/$ENTITY_CODE$> .
?transcript <http://purl.obolibrary.org/obo/SO_translates_to> ?ensembl_protein .
?reflect_protein owl:sameAs ?ensembl_protein
graph ?event {
?s ?p ?reflect_protein .
}.
{
?s a <http://lcsb.uni.lu/biokb#Chemical> .
?s owl:sameAs ?same_s .
?same_s rdfs:label ?s_label .
} UNION {
?s a <http://lcsb.uni.lu/biokb#Protein> .
?s owl:sameAs ?same_s .
?ensembl_transcript <http://purl.obolibrary.org/obo/SO_translates_to> ?same_s .
?ensembl_transcript rdfs:label ?s_label
}
?s a ?s_type .

?sentence <http://lcsb.uni.lu/biokb#hasEvent> ?event .
?sentence <http://lcsb.uni.lu/biokb#containsEntity> ?context .
?sentence <http://lcsb.uni.lu/biokb#text> ?text .
?context a <http://lcsb.uni.lu/biokb#Tissue> .
?context owl:sameAs ?same_context .
?same_context rdfs:label ?context_label .
}"""

new_query=query_with_placeholder.replace("$ENTITY_CODE$",var)

sparql = SPARQLWrapper("http://biokb.lcsb.uni.lu/sparql")

sparql.setQuery(new_query)

sparql.setReturnFormat(JSON)

result_of_query = sparql.query().convert()

return result_of_query

Var是我正在迭代的列表中的元素。 由于使用此功能我犯了一个错误

HTTPError: HTTP Error 404: File not found

EndPointNotFound: EndPointNotFound: it was impossible to connect with the endpoint in that address, check if it is correct. 

Response:
b'<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">\n<html>\n  <head>\n    <title>Error HTTP/1.1 404 File not found</title>\n  </head>\n  <body>\n    <h3>Error HTTP/1.1 404 File not found</h3><pre>\nThe requested URL was not found    URI  = \'/sparql\'\n  </pre></body></html>\n'

因此我的查询停止在第65个元素处迭代。但我仍然可以运行查询的元素和其他元素,所以我想它必须工作。

错误在哪里?以及如何纠正?

1 个答案:

答案 0 :(得分:1)

在短时间内经常向DataBase运行查询时会出现此错误。

EndPointNotFound: EndPointNotFound: it was impossible to connect with the endpoint in that address, check if it is correct. 

Response:
b'<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">\n<html>\n  <head>\n    <title>Error HTTP/1.1 404 File not found</title>\n  </head>\n  <body>\n    <h3>Error HTTP/1.1 404 File not found</h3><pre>\nThe requested URL was not found    URI  = \'/sparql\'\n  </pre></body></html>\n'

添加以下代码:

import time
time.sleep(2)

其中2 - 秒的时间。