Search code examples
pythontextauthorpubmed

Obtaining author affiliations from PubMed via Python


I am working on a Python script (modified from here and reported below) to search on PubMed the number of papers from a certain university, and download the affiliation of the collaborators. If I run the code, instead of the affiliations I get <Element 'Affiliation' at 0x106ea7e50>. Do you know how to fix this? And what should I do to download the affiliation for all the authors? Thanks!

import urllib, urllib2, sys
import xml.etree.ElementTree as ET

def chunker(seq, size):
    return (seq[pos:pos + size] for pos in xrange(0, len(seq), size))

query = '(("University of Copenhagen"[Affiliation]))# AND ("1920"[Publication Date] : "1930"[Publication Date]))'

esearch = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&mindate=2001&maxdate=2010&retmode=xml&retmax=10000000&term=%s' % (query)
handle = urllib.urlopen(esearch)
data = handle.read()

root = ET.fromstring(data)
ids = [x.text for x in root.findall("IdList/Id")]
print 'Got %d articles' % (len(ids))

for group in chunker(ids, 100):
    efetch = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?&db=pubmed&retmode=xml&id=%s" % (','.join(group))
    handle = urllib.urlopen(efetch)
    data = handle.read()

    root = ET.fromstring(data)
    for article in root.findall("PubmedArticle"):
        pmid = article.find("MedlineCitation/PMID").text
        year = article.find("MedlineCitation/Article/Journal/JournalIssue/PubDate/Year")
        if year is None: year = 'NA'
        else: year = year.text
        aulist = article.findall("MedlineCitation/Article/AuthorList/Author")
        affiliation = article.find("MedlineCitation/Article/AuthorList/Author/Affiliation")
        print pmid, year, len(aulist), affiliation

Solution

  • The reason this is occurring is that the affiliation object refers to an XML element, not a piece of text. If the string you want is contained within the value, like so:

    <affiliation>
        your_affiliation_text
    </affiliation>  
    

    you'd want to print affiliation.text.

    If the string you wanted was contained within an attribute, like so:

     <affiliation your_attribute_name="your_affiliation">
    

    you'd want to use affiliation.attrib[name].