下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
# -*- Coding: utf-8 -*-"""Created on Fri Aug 07 17:30:58 2015@author: Dreace"""import urllib2import sysimport timeimport osimport randomfrom multiprocessing.dummy import Pool as ThreadPool type_ = sys.getfilesystemenCoding()def rename(): return time.strftime("%Y%m%d%H%M%s")def rename_2(name): if len(name) == 2: name = '0' + name + '.jpg' elif len(name) == 1: name = '00' + name + '.jpg' else: name = name + '.jpg' return namedef download_pic(i): global count global time_out if Filter(i): try: content = urllib2.urlopen(i,timeout = time_out) url_content = content.read() file_name = repr(random.randint(10000,999999999)) + "_" + rename_2(repr(count)) f = open(file_name,"wb") f.write(url_content) f.close() if os.path.getsize(file_name) >= 1024*11: count += 1 else: os.remove(file_name) except Exception,e: print edef Filter(content): for line in Filter_List: if content.find(line) == -1: return Truedef get_pic(url_address): global pic_List global time_out global headers try: req = urllib2.Request(url = url_address,headers = headers) str_ = urllib2.urlopen(req,timeout = time_out).read() url_content = str_.split("\'") for i in url_content: if i.find(".jpg") != -1: pic_List.append(i) except Exception,e: print eMAX = 100count = 0time_out = 60thread_num = 50pic_List = []page_List = []pic_kind = ["hot","share","mm","taiwan","japan","model"]Filter_List = ["imgsize.ph.126.net","img.ph.126.net","img2.ph.126.net"]dir_name = "C:\Photos\"+rename()os.makedirs(dir_name)os.chdir(dir_name)start_time = time.time()url_address = "http://www.mzitu.com/model/page/"headers = {"User-Agent":" Mozilla/5.0 (windows NT 10.0; rv:39.0) Gecko/20100101 firefox/39.0"}for pic_i in pic_kind: for i in range(1,MAX + 1): page_List.append(url_address + pic_i + "/page/" + repr(i))page_pool = ThreadPool(thread_num)page_pool.map(get_pic,page_List)page_pool.close()page_pool.join()print "获取到".decode("utf-8").encode(type_),len(pic_List),"张图片,开始下载!".decode("utf-8").encode(type_)pool = ThreadPool(thread_num) pool.map(download_pic,pic_List)pool.close() pool.join()print count,"张图片保存在".decode("utf-8").encode(type_) + dir_nameprint "共耗时".decode("utf-8").encode(type_),time.time() - start_time,"s"
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的Python抓妹子图+多进程全部内容,希望文章能够帮你解决Python抓妹子图+多进程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)