Skip to main content
 首页 » 编程设计

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

2025年01月19日8tuyile006

我正在使用 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 个元素处停止迭代。但我仍然可以运行该元素和其他元素的查询,所以我想它一定可以工作。

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

请您参考如下方法:

当您在短时间内过于频繁地对数据库运行查询时,就会出现此错误。

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' 

将以下内容添加到 python 代码中:

import time 
time.sleep(2) 

其中 2 - 以秒为单位的时间。