-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchpic.py
37 lines (32 loc) · 1.09 KB
/
fetchpic.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
import urllib
import re,os
def getImages(myurl):
imgSum = 0
badImg = 0
urlStr = urllib.urlopen(myurl).read()
#hrefCmp = re.compile("""<img src=".*?" .*?>""")
#hrefCmp = re.compile("""<.*? src="(.*?)" .*?>""")
#hrefCmp = re.compile("""<img src="(.*?)"(.*?)>""")
hrefCmp = re.compile("""<img.*?src="(.*?)".*?>""") #ok
hreflist = hrefCmp.findall(urlStr)
drive = "/home/tom/pyt"
if not os.path.exists(drive):
os.mkdir(drive)
for href in hreflist:
#print href
if href.find("""http://""")==0: # must start with http
imageName = href[href.rindex("/")+1:]
try:
urllib.urlretrieve(href, os.path.join(drive,imageName))
imgSum += 1
print imageName + " OK"
except : #default
print "cannot download this image: "+imageName
#print href
else:
badImg += 1
print href
print "Success: ",imgSum," Failed: ",badImg
if __name__ == "__main__":
imgurl = raw_input("url -> ")
getImages(imgurl)