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

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

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

最佳答案

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

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 - 以秒为单位的时间。

关于python - 使用 Python 处理时 sparql 查询中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48932164/

相关文章:

rdf - SPARQL Tracker 无法运行更新,在本体中找不到属性

sparql - 来自 dbpedia 的简单 sparql 查询

python - 我应该在哪里放置配置详细信息? [Python]

Python 以内存安全的方式使用析构函数

javascript - 为使用 dbpedia 的 sparql 查询生成 url

sparql - 如何在 Wordnet 中找到 "lexical file"?

sparql - 在 sparql IN 子句中使用另一个 sparql

python - 如何根据该行的第 1 列与该行的第 2 列相比大多少来给出某些行 'points'

python - 使滚动条在 Canvas tkinter 上动态改变长度

python - 如何打开 .exe 文件?