title: Obtaining author affiliations from PubMed via Python
body: <p>I am working on a Python script (modified from <a href="http://blog.rguha.net/?p=775" rel="nofollow noreferrer">here</a> 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 <code>&lt;Element 'Affiliation' at 0x106ea7e50&gt;</code>. Do you know how to fix this? And what should I do to download the affiliation for all the authors? Thanks!</p>
  <pre><code>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 = '((&quot;University of Copenhagen&quot;[Affiliation]))# AND (&quot;1920&quot;[Publication Date] : &quot;1930&quot;[Publication Date]))'
  
  esearch = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&amp;mindate=2001&amp;maxdate=2010&amp;retmode=xml&amp;retmax=10000000&amp;term=%s' % (query)
  handle = urllib.urlopen(esearch)
  data = handle.read()
  
  root = ET.fromstring(data)
  ids = [x.text for x in root.findall(&quot;IdList/Id&quot;)]
  print 'Got %d articles' % (len(ids))
  
  for group in chunker(ids, 100):
      efetch = &quot;http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?&amp;db=pubmed&amp;retmode=xml&amp;id=%s&quot; % (','.join(group))
      handle = urllib.urlopen(efetch)
      data = handle.read()
  
      root = ET.fromstring(data)
      for article in root.findall(&quot;PubmedArticle&quot;):
          pmid = article.find(&quot;MedlineCitation/PMID&quot;).text
          year = article.find(&quot;MedlineCitation/Article/Journal/JournalIssue/PubDate/Year&quot;)
          if year is None: year = 'NA'
          else: year = year.text
          aulist = article.findall(&quot;MedlineCitation/Article/AuthorList/Author&quot;)
          affiliation = article.find(&quot;MedlineCitation/Article/AuthorList/Author/Affiliation&quot;)
          print pmid, year, len(aulist), affiliation
  </code></pre>