How Python implements bulk download of images

Source: Internet
Author: User
The example in this article describes how Python implements bulk download images. Share to everyone for your reference. The implementation method is as follows:

#!/usr/bin/env python#-*-coding:utf-8-*-' #Filename:d ownload_file.pyimport os,sysimport reimport urllibimport Urllib2base_url = ' xxx ' Array_url = list () Pic_url = List () Inner_url = List () def get_array_url (Array_url,base_url): Conten t = Urllib.urlopen (base_url). Read () Array_url_a = Re.findall (R '/rihan.*?. HTML ', content) for the URL in array_url_a:url_a = ' xxx ' +url #print url_a array_url.append (url_a) def Get_inner_url (a Rray_url,inner_url): Inner_url.append (array_url) content = Urllib.urlopen (array_url[10]). Read () content = Content.replace ("", "") Url_a = Re.findall (R ')
  • .*?
  • ', content) for i in Url_a:url = Re.findall (R ' ahref=\\ '. *?. Html\\ ' target ', i) if Len (URL) >0: # print url[0] Url_b = re.sub (R ' ahref=\\ "," ', Url[0]) # print Url_b Url_c = re.sub (R ' \ \ ' target ', ' ', url_b) Url_c = ' http://xxx/' +re.sub (R '/.*/', ', Url_c) inner_url.append (Url_c ) del Inner_url[1] # print Inner_urldef get_pic_url (pic_url,inner_url,array_url): content = Urllib.urlopen (array_url). R EAD () pic_url_a = Re.findall (R ' center.*?). JPG ', content) print ' bbbbbbbbb ', Len (pic_url_a) pic_url_a = Re.findall (R ' Http://.*.jpg ', pic_url_a[0]) pic_url.append ( Pic_url_a[0]) j=2 for i in INNER_URL:JJ = '/' +str (j) + '. jpg ' pic = re.sub (R '/1.jpg ', jj,pic_url_a[0]) Pic_url.ap Pend (pic) j = j+1 del pic_url[-1] for i in Pic_url:print idef urlcallback (a,b,c): "" "" "" " , downloaded data block B, size of data block C, remote file Size "" "Print" callback "prec=100.0*a*b/c if < prec:prec=100 print"%.2f%% "% (Prec,) def download (img_url,file_num): foR img in img_url:print img img_name = re.sub (R ' http://.*/', ', img) path = ' c:/' +str (file_num) + '/' +img_name Urllib.urlretrieve (Img,path,urlcallback) get_array_url (array_url,base_url) file_num = 3#download (pic_url,file_num) Get_inner_url (Url,inner_url) Get_pic_url (pic_url,inner_url,url) "For url" in array_url:print URL # get_inner_url (URL, Inner_url) # Get_pic_url (Pic_url,inner_url) get_inner_url (url,inner_url) Get_pic_url (Pic_url,inner_url,url) Download (pic_url,file_num) File_num = File_num+1 del inner_url[:] del pic_url[:] "

    Hopefully this article will help you with Python programming.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.