Parsing simple html in Python
Posted by jpluimers on 2018/11/29
Was working to get fritzcap to emit a list of interfaces so I could specify which one to capture.
For that I needed to parse the output of http://fritz.box/capture.lua which consists of HTML fragments like below.
What I needed was for each consecutive entries of [WayBack] th and first [WayBack] button tags:
- content of the
thtag - content of the
valueattribute of thebuttontag having atype="submit"attribute andname=startattribute
So before starting to work on it, I created [WayBack] In order to fix #5, print a list of available interfaces to potentially capture from · Issue #6 · jpluimers/fritzcap
The goal was to get a series of key/value pairs:
4-138 = AP2 (2.4 + 5 GHz, ath1) - Interface 1
4-137 = AP2 (2.4 + 5 GHz, ath1) - Interface 0
4-132 = AP (2.4 GHz, ath0) - Interface 1
4-131 = AP (2.4 GHz, ath0) - Interface 0
4-129 = HW (2.4 GHz, wifi0) - Interface 0
4-128 = WLAN Management Traffic - Interface 0a
So I built a class descending from [WayBack] HTMLParser — Simple HTML and XHTML parser that ships with the [WayBack] Python standard libraries.
If in the future I need more complex HTML parsing, then these links will help me choosing more feature rich parsers:
- [WayBack] Python HTML Parser Performance
- [WayBack] HTML Scraping — The Hitchhiker’s Guide to Python
- [WayBack] Parsing HTML using Python – Stack Overflow
Back to the HTMLParser descendant in interfaces_dumper.py which can basically be condensed down to the code below.
handle_datais called for both start tags and end tags. Thethvalue indatais only present in the start tag (at the time of end tag thedatais empty), so you need to keep track of bothlast_start_tagandlast_end_tag.handle_endtagmaintainslast_end_tagto helphandle_data.handle_starttagmaintainslast_start_tagto helphandle_dataand also handles thebuttonbehaviour.- The
buttonis only relevant if it hastype="submit"andname="start"and avalueattribute in that order. - Output is in
datawhich is an array ofkey/valuepairs.
- The
class CaptureLuaHtmlParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.last_end_tag = ""
self.last_start_tag = ""
self.last_th_data = ""
self.data = [] # output list of (key,value) pairs
self.recording = 0
def handle_starttag(self, tag, attrs):
self.last_start_tag = tag
if tag != 'button':
return
button_attribute_type_submit = False
button_attribute_name_start = False
for name, value in attrs:
if (name == 'type') and (value == 'submit'):
button_attribute_type_submit = True
if button_attribute_type_submit and (name == 'name') and (value == 'start'):
button_attribute_name_start = True
if button_attribute_type_submit and button_attribute_name_start and (name == 'value'):
self.data.append((value, self.last_th_data)) # (button value value, th content)
def handle_endtag(self, tag):
self.last_end_tag = tag
def handle_data(self, data):
if (self.last_start_tag == 'th') and (self.last_end_tag != 'th'):
self.last_th_data = data
With all that, it’s pretty simple to get the data out and display the key value pairs using a loop:
parser = CaptureLuaHtmlParser()
parser.feed(html_content)
self.logger.info("Fritz!Box interfaces from %s: key = value" % url)
keyValuePairs = sorted(parser.data)
for (key, value) in keyValuePairs:
self.logger.info(" %-*s= %s" % (20,key, value))
First it sorts the output, which does not use a key as sorted gets that by itself: [WayBack] 2. Built-in Functions — sorted
This uses the built in precent string formatting operator explained here:
- [WayBack] 5. Built-in Types — String Formatting Operations
- [WayBack] Python spacing and aligning strings – Stack Overflow
| <h3>WLAN</h3><table class="zebra"> | |
| <tr> | |
| <th>AP (2.4 GHz, ath0) - Interface 1</th> | |
| <td class="buttonrow"> | |
| <button type="submit" name="start" id="uiStart_132" value="4-132">Start</button> | |
| </td> | |
| <td class="imgcol" id="uiImage_132"></td> | |
| <td class="buttonrow"><button type="submit" name="stop" id="uiStop_132" value="4;132;AP (2.4 GHz, ath0)">Stop</button></td> | |
| </tr> | |
| <tr> | |
| <th>AP (2.4 GHz, ath0) - Interface 0</th> | |
| <td class="buttonrow"> | |
| <button type="submit" name="start" id="uiStart_131" value="4-131">Start</button> | |
| </td> | |
| <td class="imgcol" id="uiImage_131"></td> | |
| <td class="buttonrow"><button type="submit" name="stop" id="uiStop_131" value="4;131;AP (2.4 GHz, ath0)">Stop</button></td> | |
| </tr> | |
| <tr> | |
| <th>HW (2.4 GHz, wifi0) - Interface 0</th> | |
| <td class="buttonrow"> | |
| <button type="submit" name="start" id="uiStart_129" value="4-129">Start</button> | |
| </td> | |
| <td class="imgcol" id="uiImage_129"></td> | |
| <td class="buttonrow"><button type="submit" name="stop" id="uiStop_129" value="4;129;HW (2.4 GHz, wifi0)">Stop</button></td> | |
| </tr> | |
| <tr> | |
| <th>WLAN Management Traffic - Interface 0</th> | |
| <td class="buttonrow"> | |
| <button type="submit" name="start" id="uiStart_128" value="4-128">Start</button> | |
| </td> | |
| <td class="imgcol" id="uiImage_128"></td> | |
| <td class="buttonrow"><button type="submit" name="stop" id="uiStop_128" value="4;128;WLAN Management Traffic">Stop</button></td> | |
| </tr> | |
| </table> |
–jeroen






Leave a Comment