英文:
BioPython "urllib.error.HTTPError: HTTP Error 400: Bad Request" error message
问题
我想使用Biopython从Pubmed检索数据。
我有一个关键词和要收集数据的目标时间段。
from Bio import Entrez
from io import StringIO
import csv
# 设置电子邮件和API密钥
Entrez.email = 'helloworld@gmail.com'
Entrez.api_key = 'helloworld'
# 定义搜索条件和时间窗口
search_term = 'cancer'
start_date = '2015/01/01'
end_date = '2015/08/30'
# 定义搜索查询
query = f'{search_term}[Title/Abstract] AND ("{start_date}"[Date - Publication] : "{end_date}"[Date - Publication])'
# 使用查询搜索PUBMED
handle = Entrez.esearch(db='pubmed', term=query, retmax=10000)
record = Entrez.read(handle)
id_list = record['IdList']
然而,handle
这一行一直显示错误消息。
错误消息
如何解决这个问题?请帮助我。
英文:
I want to retrieve data from Pubmed using biopython.
I have a key word and the target period to collect the data.
from Bio import Entrez
from io import StringIO
import csv
# Set email and API key
Entrez.email = 'helloworld@gmail.com'
Entrez.api_key = 'helloworld'
# Define search terms and time window
search_term = 'cancer'
start_date = '2015/01/01'
end_date = '2015/08/30'
# Define search query
query = f'{search_term}[Title/Abstract] AND ("{start_date}"[Date - Publication] : "{end_date}"[Date - Publication])'
# Search PUBMED using the query
handle = Entrez.esearch(db='pubmed', term=query, retmax=10000)
record = Entrez.read(handle)
id_list = record['IdList']
However, the line of handle keeps showing me the error message.
error message
How can I solve this issue? please help me.
答案1
得分: 1
需要使用真正的API密钥。登录到NCBI后,可以通过Google帐号等方式快速创建API密钥,您可以在此处创建API密钥:https://www.ncbi.nlm.nih.gov/account/settings/。
当我使用我的Entrez.api_key执行您的脚本时,我得到了正确的结果(我不需要更新电子邮件地址,只更新密钥就足够了)。我通过在脚本末尾添加print(len(id_list))
来进行检查。它给我返回了9999(这可能表明搜索查询仍然有点宽泛)。
当输入错误的API密钥时,我也收到了urllib.error.HTTPError: HTTP Error 400: Bad Request
的错误。
英文:
You need to use a real API key. When logged in on ncbi - which can quickly be done with a google account for example - you can create an API key here: https://www.ncbi.nlm.nih.gov/account/settings/.
When I executed your script with my API key for Entrez.api_key, I got a correct result (I did not need to also update the email address, key was enough). I checked by adding a print(len(id_list))
at the end of your script. It gave me 9999 (which might indicate that the search query is still a bit broad).
When entering a wrong API key I also received a urllib.error.HTTPError: HTTP Error 400: Bad Request
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论