-
Notifications
You must be signed in to change notification settings - Fork 26
/
example.py
executable file
·83 lines (68 loc) · 2.31 KB
/
example.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
import AdvancedHTMLParser
if __name__ == '__main__':
parser = AdvancedHTMLParser.AdvancedHTMLParser()
parser.parseStr('''
<html>
<head>
<title>HEllo</title>
</head>
<body>
<div id="container1" class="abc">
<div name="items">
<span name="price">1.96</span>
<span name="itemName">Sponges</span>
</div>
<div name="items">
<span name="price">3.55</span>
<span name="itemName">Turtles</span>
</div>
<div name="items">
<span name="price" class="something" >6.55</span>
<img src="/images/cheddar.png" style="width: 64px; height: 64px;" />
<span name="itemName">Cheese</span>
</div>
</div>
<div id="images">
<img src="/abc.gif" name="image" />
<img src="/abc2.gif" name="image" />
</div>
<div id="saleSection" style="background-color: blue">
<div name="items">
<span name="itemName">Pudding Cups</span>
<span name="price">1.60</span>
</div>
<hr />
<div name="items" class="limited-supplies" >
<span name="itemName">Gold Brick</span>
<span name="price">214.55</span>
<b style="margin-left: 10px">LIMITED QUANTITIES: <span id="item_5123523_remain">130</span></b>
</div>
</body>
</html>
''')
# Get all items by name
items = parser.getElementsByName('items')
# Parse some arbitrary html
parser2 = AdvancedHTMLParser.AdvancedHTMLParser()
parser2.parseStr('<div name="items"> <span name="itemName">Coop</span><span name="price">1.44</span></div>')
# Append a new item to the list
items[0].parentNode.appendChild(parser2.getRoot())
items = parser.getElementsByName('items')
print ( "Items less than $4.00: ")
print ( "-----------------------\n")
#import pdb; pdb.set_trace()
for item in items:
priceEm = item.getElementsByName('price')[0]
priceValue = round(float(priceEm.innerHTML.strip()), 2)
if priceValue < 4.00:
name = priceEm.getPeersByName('itemName')[0].innerHTML.strip()
print ( "%s - $%.2f" %(name, priceValue) )
# OUTPUT:
# Items less than $4.00:
# -----------------------
#
# Sponges - $1.96
# Turtles - $3.55
# Coop - $1.44
# Pudding Cups - $1.60