Skip to content

Instantly share code, notes, and snippets.

@alcantarar
Forked from bonzanini/search_biopython.py
Last active June 8, 2018 21:01
Show Gist options
  • Save alcantarar/13eae5d3c841d678b68e54ac9ebb609f to your computer and use it in GitHub Desktop.
Save alcantarar/13eae5d3c841d678b68e54ac9ebb609f to your computer and use it in GitHub Desktop.
Searching PubMed with Biopython
# you need to install Biopython:
# pip install biopython
# Full discussion:
# https://marcobonzanini.wordpress.com/2015/01/12/searching-pubmed-with-python/
from Bio import Entrez
def search(query):
Entrez.email = 'your.email@example.com'
handle = Entrez.esearch(db='pubmed',
sort='relevance',
retmax='20',
retmode='xml',
term=query)
results = Entrez.read(handle)
return results
def fetch_details(id_list):
ids = ','.join(id_list)
Entrez.email = 'your.email@example.com'
handle = Entrez.efetch(db='pubmed',
retmode='xml',
id=ids)
results = Entrez.read(handle)
return results
if __name__ == '__main__':
results = search('fever')
id_list = results['IdList']
papers = fetch_details(id_list)
for i, paper in enumerate(papers['PubmedArticle']):
print("%d) %s" % (i+1, paper['MedlineCitation']['Article']['ArticleTitle']))
# Pretty print the first paper in full
#import json
#print(json.dumps(papers[0], indent=2, separators=(',', ':')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment