-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlParsing.py
More file actions
41 lines (39 loc) · 1.08 KB
/
htmlParsing.py
File metadata and controls
41 lines (39 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from HTMLParser import HTMLParser
class FormDefaultParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.form_defaults_map = {}
self.prev_select = None
def handle_starttag(self, tag, attrs):
if tag == "select":
for attr in attrs:
if attr[0] == 'name':
self.prev_select = attr[1]
elif tag == "option" and self.prev_select is not None:
selected = False
value = None
for attr in attrs:
if attr[0] == 'value':
value = attr[1]
elif attr[0] == 'selected':
selected = True
if selected:
self.form_defaults_map[self.prev_select] = value
elif tag == "input":
name = None
value = None
checked = False
f_type = None
for attr in attrs:
if attr[0] == 'name':
name = attr[1]
elif attr[0] == 'value':
value = attr[1]
elif attr[0] == 'type':
f_type = attr[1]
elif attr[0] == 'checked':
checked = True
if f_type == 'checkbox' and not checked:
return # don't add the value if the checkbox is not selected
if name is not None and value is not None:
self.form_defaults_map[name] = value