|
Size: 1893
Comment:
|
Size: 1966
Comment:
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 1: | Line 1: |
| = Python XML DOM minidom = | = Python XML DOM Minidom = |
| Line 8: | Line 8: |
| Line 24: | Line 26: |
| === Scrape HTML tables === {{{ for table in document.getElementsByTagName("table"): for row in table.getElementsByTagName("tr"): if row.firstChild is not None and row.firstChild.nodeName is not None and row.firstChild.nodeName=="th": for header in row.childNodes: data[0].append(header.nodeValue if header.nodeValue is not None else "") else: data.append([]) for cell in row.childNodes: data[-1].append(cell.nodeValue if cell.nodeValue is not None else "") }}} |
|
| Line 51: | Line 38: |
| }}} === Scrape HTML tables === {{{ def recurse_text(node): buffer = "" for child in node.childNodes: if child.nodeType == minidom.Node.TEXT_NODE: buffer += child.data else: buffer += recurse_text(child) return buffer for table in document.getElementsByTagName("table"): for row in table.getElementsByTagName("tr"): data.append([]) for header_cell in row.getElementsByTagName("th"): data[0].append(recurse_text(header_cell)) for cell in row.getElementsByTagName("td"): data[-1].append(recurse_text(cell)) |
Python XML DOM Minidom
Contents
Usage
Parsing a file
from xml.dom import minidom document = minidom.parse(filename)
If the XML file uses namespaces, it can be easier to disable that feature in the parser.
from xml.dom import minidom, expatbuilder document = expatbuilder.parse(filename, False)
Traverse all nodes
def recurse_print(node):
if node.nodeType == minidom.Node.TEXT_NODE:
print(node.data)
else:
for child in node.childNodes:
recurse_print(child)
recurse_print(document)
Scrape HTML tables
def recurse_text(node):
buffer = ""
for child in node.childNodes:
if child.nodeType == minidom.Node.TEXT_NODE:
buffer += child.data
else:
buffer += recurse_text(child)
return buffer
for table in document.getElementsByTagName("table"):
for row in table.getElementsByTagName("tr"):
data.append([])
for header_cell in row.getElementsByTagName("th"):
data[0].append(recurse_text(header_cell))
for cell in row.getElementsByTagName("td"):
data[-1].append(recurse_text(cell))
Scrubbing the DOM
It can be useful to scrub the DOM of unhelpful or useless components.
To remove attributes, try:
if node.hasAttribute("hidden"):
node.removeAttribute("hidden")To remove nodes, try:
for child in node.childNodes:
if child.hasAttribute("hidden"):
node.removeChild(child)
child.unlink()To replace nodes, as with comments, try:
replacement = document.createComment("scrubbed useless node")
# alternatively, createTextNode or createElement
for child in node.childNodes:
if child.hasAttribute("hidden"):
node.replaceChild(child, replacement)