蟲師-https://www.cnblogs.com/fnng/p/3581433.html
xml.dom篇
DOM是Document Object Model的簡稱,XML 文檔的高級樹型表示。該模型並非只針對 Python,而是一種普通XML 模型。Python 的 DOM 包是基於 SAX 構建的,並且包括在 Python 2.0 的標准 XML 支持里。
一、xml.dom的簡單介紹
1、主要方法:
minidom.parse(filename):加載讀取XML文件
doc.documentElement:獲取XML文檔對象
node.getAttribute(AttributeName):獲取XML節點屬性值
node.getElementsByTagName(TagName):獲取XML節點對象集合
node.childNodes :返回子節點列表。
node.childNodes[index].nodeValue:獲取XML節點值
node.firstChild:訪問第一個節點,等價於pagexml.childNodes[0]
返回Node節點的xml表示的文本:
doc = minidom.parse(filename)
doc.toxml('UTF-8')
訪問元素屬性:
Node.attributes["id"]
a.name #就是上面的 "id"
a.value #屬性的值