说明蓝色=命令名称

      浅绿=命令参数

      浅蓝=选项

      紫色=目录

      系统环境:CentOS  5.5  x86_64

      python版本:Python 2.7.3

 

  1. #!/usr/bin/env python 
  2. #-*- coding:utf-8 -*-   
  3. #Author:left_left   
  4. import sys 
  5. import os 
  6. import urllib2 
  7. import threading 
  8. from optparse import OptionParser 
  9.  
  10. mutex = threading.Lock() 
  11. logfile = open("wget_pic.log""w"
  12.  
  13. class GetPic(threading.Thread): 
  14.     def __init__(self, dirname, uris, ip): 
  15.         threading.Thread.__init__(self
  16.         self.uris = uris 
  17.         self.dirname = dirname 
  18.         self.ip = ip 
  19.      
  20.     def run(self): 
  21.         global logfile, mutex  
  22.         for uri in self.uris: 
  23.             if uri.startswith("http"): 
  24.                 try
  25.                     if self.ip: 
  26.                         s_uri = uri.split("/"
  27.                         host = s_uri[2
  28.                         s_uri[2] = self.ip 
  29.                         request = urllib2.Request("/".join(s_uri), 
  30.                         headers = {"Host":host}) 
  31.                         response = urllib2.urlopen(request, timeout = 5
  32.                     else
  33.                         response = urllib2.urlopen(uri, timeout = 5
  34.                 except urllib2.HTTPError, e: 
  35.                     if mutex.acquire(): 
  36.                         print uri 
  37.                         logfile.write(uri) 
  38.                         mutex.release()  
  39.                 except urllib2.URLError, e: 
  40.                     print e 
  41.                 else:  
  42.                     pic_name = uri.split("/")[-1].strip() 
  43.                     f = open(self.dirname + "/" + pic_name, 'wb'
  44.                     f.write(response.read()) 
  45.                     f.close() 
  46.  
  47. def parse(): 
  48.     p = OptionParser() 
  49.      
  50.     p.add_option('-p''--ipaddr', dest = "ip"
  51.     p.add_option('-g''--log', dest = 'log'
  52.     p.add_option('-d''--to-dir', dest = 'dir'
  53.     p.add_option('-t''--threads', dest = 'threads', default = 5
  54.      
  55.     return p 
  56.  
  57. def main(): 
  58.     global logfile 
  59.     p = parse() 
  60.     options, args = p.parse_args() 
  61.  
  62.     for file in args: 
  63.         if options.dir: 
  64.             dirname = options.dir 
  65.         else
  66.             dirname = file.split("/")[-1].split('.')[0
  67.          
  68.         try
  69.             os.mkdir(dirname) 
  70.         except OSError, e: 
  71.             #print "Mkdir Error:%s" % e 
  72.             #exit(1) 
  73.             pass 
  74.         if options.log: 
  75.             try
  76.                 logfile = open(options.log, "w"
  77.             except IOError, e: 
  78.                 print e 
  79.                 exit(1)  
  80.  
  81.         thread_num = int(options.threads) 
  82.         uris = open(file).readlines() 
  83.         members = len(uris)/thread_num 
  84.         threads = [] 
  85.  
  86.         for i in xrange(thread_num): 
  87.             threads.append(GetPic(dirname, 
  88.             uris[i*members:(i+1)*members], options.ip)) 
  89.          
  90.         if len(uris)%thread_num: 
  91.             threads.append(GetPic(dirname, 
  92.             uris[thread_num*members:], options.ip)) 
  93.          
  94.         for t in threads: 
  95.             t.setDaemon(True
  96.             t.start() 
  97.  
  98.         for t in threads: 
  99.             t.join() 
  100. logfile.close()
  101.  
  102. if __name__ == "__main__"
  103.     main() 

效果如图:

 

py批量图片抓取_批量抓图

day_130412.txt内容:

py批量图片抓取_urllib2_02