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.
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
.