[問題] 請教elementtree的問題

看板Python作者 (vanchy)時間11年前 (2014/12/31 14:29), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/1
大家好!我是一位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

12/31 17:24, , 1F
attrib抓的是<>裡面的屬性 如<firstname value='a'/>
12/31 17:24, 1F

12/31 17:25, , 2F
抓tag跟它的值用 gchild.tag, gchild.text
12/31 17:25, 2F

01/02 00:55, , 3F
感謝樓上^^原來是用錯method
01/02 00:55, 3F
文章代碼(AID): #1KevUrjU (Python)
文章代碼(AID): #1KevUrjU (Python)