Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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):
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=(',', ':')))
@hongtao510

This comment has been minimized.

Copy link

hongtao510 commented May 4, 2017

Have you encountered the issue that the server will cut you off after querying ~2000 IDs?

@ThitherShore

This comment has been minimized.

Copy link

ThitherShore commented Jun 20, 2017

Should the Line 32, 33 be changed into:
for i, paper in enumerate(papers['PubmedArticle']): print("%d) %s" % (i+1, paper['MedlineCitation']['Article']['ArticleTitle']))

@nmagee

This comment has been minimized.

Copy link

nmagee commented Jun 24, 2017

@ThitherShore is correct - I can verify that suggested fix makes this gist functional.

@lemonsoftltd

This comment has been minimized.

Copy link

lemonsoftltd commented Aug 21, 2017

@hongtao510 we download 100 articles in an infinite loop. We see no blocking info since one week.

@BadrulAlom

This comment has been minimized.

Copy link

BadrulAlom commented Sep 17, 2017

Please update code with ThitherShore's comment.

@reiaoki

This comment has been minimized.

Copy link

reiaoki commented Feb 23, 2018

Whenever there is a "+" sing in the content (for example ArticleTitle or AbstractText), it returns a string only after the "+" sign. Does anyone have a way to get around this?
e.g. The title "The Clinicopathological and Prognostic Implications of FoxP3+ Regulatory T Cells in Patients with Colorectal Cancer: A Meta-Analysis." will return "+ Regulatory T Cells in Patients with Colorectal Cancer: A Meta-Analysis"

@TomazAlexandre

This comment has been minimized.

Copy link

TomazAlexandre commented Apr 2, 2018

It looks like the format returned by the efetch method is slightly different now

If you replace papers with papers[‘PubmedArticle’] you should get the list or papers,

@makerspaze

This comment has been minimized.

Copy link

makerspaze commented Jun 4, 2018

@ThitherShore is correct we should use enumerate(papers['PubmedArticle']) instead of enumerate(papers)

@gunnarklee

This comment has been minimized.

Copy link

gunnarklee commented Dec 16, 2019

for line 36

print(json.dumps(papers['PubmedArticle'][0], indent=2, separators=(',', ':')))
instead of:
print(json.dumps(papers[0], indent=2, separators=(',', ':')))

@prashantkum7

This comment has been minimized.

Copy link

prashantkum7 commented Mar 20, 2020

I get abstracts and not the full text. Any reason why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.