简体   繁体   English

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

[英]An error in the sparql query when processing with Python

I am processing a sparql query with Python.我正在使用 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 is an element from a list I am iterating through. Var 是我正在迭代的列表中的一个元素。 As a result of using this function I got a mistake由于使用此功能,我犯了一个错误

HTTPError: HTTP Error 404: File not found

and

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'

And thus my query stops iterating at 65th element.因此我的查询在第 65 个元素处停止迭代。 But I still can run the query for that elements and further ones so I suppose it must work.但我仍然可以运行该元素和更多元素的查询,所以我想它必须工作。

Where is the mistake?错误在哪里? And how it could be corrected?以及如何纠正?

This mistake appears when you run the query to DataBase too often during short period of time.当您在短时间内对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'

Add to python code the following:将以下内容添加到 python 代码中:

import time
time.sleep(2)

Where 2 - time in seconds.其中 2 - 以秒为单位的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM