0

I am using urllib and ElementTree to parse XML API calls from pubmed.

An example of this is:

#Imports Modules that can send requests to URLs 
#Python Version 3.4 Using IEP (Interactive Editor for Python) as IDE  
import urllib.request 
import urllib.parse 
import re 
import xml.etree.ElementTree as ET 
from urllib import request 

#Obtain API Call and assign Element Object to Root
id_request = urllib.request.urlopen('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id=1757056')
id_pubmed = id_request.read()
root = ET.fromstring(id_pubmed)

I now have been able to use Element Tree to import the data to the object root from ET.fromstring. My issue now, is that I am having trouble finding interesting elements from this object.

I am referring to: https://docs.python.org/2/library/xml.etree.elementtree.html and my XML format looks like: http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id=1757056

I have tried:

#Parse Attempts.  Nothing returned.
for author in root.iter('Author'):
   print (author.attrib)

As well as

#No Return for author
for author in root.findall('Id'):
   author = author.find('author').text
   print (author)
4

2 回答 2

1

尝试按标签迭代

for author in root.iter('Item'):
    if author.attrib['Name'] == 'Author':
    print("Success") 

或者:

author_list = [x for x in root.iter('Item') if x.attrib['Name'] == 'Author']

不知道能不能按属性迭代

于 2015-08-25T14:47:08.757 回答
0

.attrib方法返回标签内的值。我认为您可能想使用其中一个.tag.text代替。我不确定您要从这棵树中提取什么数据,但您也可以遍历该author值。

编辑:嗯 esummaryResult 标签似乎毫无意义,除非你会有更多的 DocSum 标签。但是你想要的信息是你的.text价值。尝试打印author.tag,也许您可​​以检查当前正在迭代的返回值。

于 2015-08-25T14:37:47.917 回答