[问题] 请教elementtree的问题

楼主: vanchy (vanchy)   2014-12-31 14:29:07
大家好!我是一位python的新手
目前使用ElementTree遇到了一些问题
想请教各位解决的方法
目标:尝试自己爬出想要的资料
档案格式:xml,http://imgur.com/rxp5q7A
问题:使用ElementTree爬资料时,印不出tag中的值,一直印出空的dictionary
程式码:
import xml.etree.ElementTree as ET
import xml.etree.cElementTree as ET
tree = ET.parse('councillors.xml')
root = tree.getroot()
for child in root:
for gchild in child:
print gchild.attrib
谢谢各位帮我解答^^
作者: Seudo (进击的三笠)   2014-12-31 17:24:00
attrib抓的是<>里面的属性 如<firstname value='a'/>抓tag跟它的值用 gchild.tag, gchild.text
楼主: vanchy (vanchy)   2013-01-02 00:55:00
感谢楼上^^原来是用错method

Links booklink

Contact Us: admin [ a t ] ucptt.com