作者vanchy (vanchy)
看板Python
標題[問題] 請教elementtree的問題
時間Wed Dec 31 14:29:07 2014
大家好!我是一位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
謝謝各位幫我解答^^
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.117.247.80
※ 文章網址: http://www.ptt.cc/bbs/Python/M.1420007349.A.B5E.html
推 Seudo: attrib抓的是<>裡面的屬性 如<firstname value='a'/> 12/31 17:24
→ Seudo: 抓tag跟它的值用 gchild.tag, gchild.text 12/31 17:25
→ vanchy: 感謝樓上^^原來是用錯method 01/02 00:55