-
Notifications
You must be signed in to change notification settings - Fork 1
/
BingHtmlParser.py
59 lines (49 loc) · 1.61 KB
/
BingHtmlParser.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
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
try:
from html.parser import HTMLParser
except ImportError:
from HTMLParser import HTMLParser
class BingHtmlParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.info_found = False
self.info = ""
self.copyright_found = False
self.copyright = ""
self.image_url = ""
def handle_starttag(self, tag, attrs):
if tag == 'div':
for name, value in attrs:
if name == 'class' and value == 'copyright':
self.copyright_found = True
if tag == 'a':
for name, value in attrs:
if name == 'class' and value == 'title':
self.info_found = True
if name == 'download' and value == 'BingWallpaper.jpg':
for name2, href in attrs:
if name2 == 'href':
self.image_url = href
def handle_endtag(self, tag):
if self.info_found:
self.info_found = False
if self.copyright_found:
self.copyright_found = False
def handle_data(self, data):
if self.info_found:
self.info = data
if self.copyright_found:
self.copyright = data
if __name__ == "__main__":
# f = urlopen('https://www.bing.com/?mkt=zh-cn')
f = urlopen('https://www.bing.com/?mkt=en-us')
html = f.read()
p = BingHtmlParser()
p.feed(str(html))
print(p.image_url)
print(p.info)
print(p.copyright)
p.close()