3

Can you tell me how I can obtain the 5 newest articles from PubMed that contain the word 'obesity' and return the authors, the title, the date, doi and PubMed PMID of each paper using Python? Thank you in advance

EDIT:

My try so far. I believe this is the wanted result but i need to obtain the correct pubmed ids in order to run the function

from Bio.Entrez import efetch
from Bio import Entrez
Entrez.email = 'name@example.com'

def print_abstract(pmid):
    handle = efetch(db='pubmed', id=pmid, retmode='text', rettype='abstract')
    print(handle.read())

print_abstract(pmid)
dspencer
  • 4,297
  • 4
  • 22
  • 43
EAS
  • 39
  • 5
  • 1
    we can tell you, but it wont teach you anything. We would need a LINK to the website and your attempt. – Fallenreaper Mar 15 '20 at 23:21
  • @Fallenreaper I believe this function may work just fine if i mange to get the correct ids according to date and term. Of course, there can be a better way that i haven't thought of yet – EAS Mar 15 '20 at 23:33

1 Answers1

2

You can use the esearch function from Bio.Entrez to find the UIDs of the most recent PubMed entries matching a query:

handle = Entrez.esearch(db="pubmed", term="obesity", retmax=5)
results = Entrez.read(handle)                                                   
print(results["IdList"])

By specifying term, you can provide a query string - from your question, you're looking for entries related to "obesity". retmax allows us to configure how many UIDs should be returned (default is 20).

When I run this now, I get the following UIDs:

['32171135', '32171042', '32170999', '32170940', '32170929']

At the time of writing, these correspond to the most recent entry UIDs when you perform the PubMed search manually. They are sorted with the most recent entry first.

Using these UIDs, your print_abstract function performs as expected.

dspencer
  • 4,297
  • 4
  • 22
  • 43