Graphic spiderman code written in Python

  • 2020-04-02 09:36:15
  • OfStack


#coding=utf-8 

import os 
import sys 
import re 
import urllib 

URL_REG = re.compile(r'(http://[^///]+)', re.I) 
IMG_REG = re.compile(r'<img[^>]*?src=([/'"])([^/1]*?)/1', re.I) 

def download(dir, url): 
''' Download the images from the web page  

@dir  Save to the local path  
@url  Web page url 
''' 
global URL_REG, IMG_REG 

m = URL_REG.match(url) 
if not m: 
print '[Error]Invalid URL: ', url 
return 
host = m.group(1) 

if not os.path.isdir(dir): 
os.mkdir(dir) 

#  To obtain html, To extract the image url 
html = urllib.urlopen(url).read() 
imgs = [item[1].lower() for item in IMG_REG.findall(html)] 
f = lambda path: path if path.startswith('http://') else / 
host + path if path.startswith('/') else url + '/' + path 
imgs = list(set(map(f, imgs))) 
print '[Info]Find %d images.' % len(imgs) 

#  Download the pictures  
for idx, img in enumerate(imgs): 
name = img.split('/')[-1] 
path = os.path.join(dir, name) 
try: 
print '[Info]Download(%d): %s'% (idx + 1, img) 
urllib.urlretrieve(img, path) 
except: 
print "[Error]Cant't download(%d): %s" % (idx + 1, img) 

def main(): 
if len(sys.argv) != 3: 
print 'Invalid argument count.' 
return 
dir, url = sys.argv[1:] 
download(dir, url) 

if __name__ == '__main__': 
# download('D://Imgs', 'http://www.163.com') 
main()


Related articles: