下面是我的示例代码,在后台我用wget下载statsxml.jsp,然后解析xml.我现在的问题是我需要解析多个XML URL,正如您在下面的代码中看到的那样,我使用的是单个文件.怎么做到这一点?
import xml.etree.cElementTree as ET
tree = ET.ElementTree(file='statsxml.jsp')
root = tree.getroot()
root.tag, root.attrib
print "root subelements: ", root.getchildren()
root.getchildren()[0][1]
root.getchildren()[0][4].getchildren()
for component in tree.iterfind('Component name'):
print component.attrib['name']
解决方法:
您可以使用urllib2以相同的方式下载和解析文件.对于例如前几行将更改为:
import xml.etree.cElementTree as ET
import urllib2
for i in range(3):
tree = ET.ElementTree(file=urllib2.urlopen('http://www.trion%i.com:6060/stat.xml' % i ))
root = tree.getroot()
root.tag, root.attrib
# Rest of your code goes here....
标签:python,xml,xml-parsing,elementtree
来源: https://codeday.me/bug/20190825/1717371.html