Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class StatusParser(html.parser.HTMLParser):
- def __init__(self):
- html.parser.HTMLParser.__init__(self)
- self.result = {}
- self._td = None # in a TD? class or 'yes'
- self._h3 = False # in a H3?
- self._key = None # content of prev. TD with class
- self._tab = False # skip next </table> ?
- self._mount = None # mount name
- self._value = None # field value
- def handle_starttag(self, tag, attrs):
- #print('{} --- {} --- {}', self, tag, attrs)
- if tag == 'td':
- self._td = 'yes'
- if len(attrs) > 0:
- if attrs[0][0] == 'class':
- self._td = attrs[0][1]
- if (self._td == 'streamdata'):
- self._value = ''
- elif tag == 'h3' and self._td:
- self._h3 = True
- self._tab = True
- def handle_endtag(self, tag):
- if tag == 'td':
- if self._value:
- self.result[self._mount][self._key[:-1]] = self._value
- self._value = None
- self._td = None
- elif tag == 'h3':
- self._h3 = False
- elif tag == 'table':
- if not self._tab:
- self._mount = None
- self._tab = False
- def handle_data(self, data):
- if self._h3 and self._td:
- self._mount = data.split(' ')[2]
- self.result[self._mount] = OrderedMultiDict()
- elif self._td and self._mount:
- if self._td == 'streamdata':
- self._value += data
- else:
- self._key = data
- def handle_charref(self, name):
- if self._value:
- if name.startswith('x'):
- c = chr(int(name[1:], 16))
- else:
- c = chr(int(name))
- self._value += c
- def handle_entityref(self, name):
- if self._value:
- self._value += chr(name2codepoint[name])
Advertisement
Add Comment
Please, Sign In to add comment