forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpictures.py
More file actions
43 lines (27 loc) · 846 Bytes
/
Copy pathpictures.py
File metadata and controls
43 lines (27 loc) · 846 Bytes
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
#!/usr/bin/env python
from HTMLParser import HTMLParser
import urllib2
class MyHtmlParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.picUrls = []
def handle_starttag(self, tag, attrs):
if tag == "img":
if len(attrs) > 0:
for key, value in attrs:
if key == "src":
self.picUrls.append(value)
def Pic(picurls):
count = 1
for picurl in picurls:
conn = urllib2.urlopen(picurl)
f = open(str(count)+".jpg", 'wb')
f.write(conn.read())
f.close()
count += 1
if __name__ == '__main__':
html = urllib2.urlopen("http://tieba.baidu.com/p/2166231880").read()
htmlParser = MyHtmlParser()
htmlParser.feed(html)
htmlParser.close()
Pic(htmlParser.picUrls)