How to batch download images using Python

Source: Internet
Author: User
This article mainly introduces how to download images in batches using Python. It involves the techniques for obtaining URLs and downloading images using URL and regular expression Matching Based on urllib, urllib2, and re modules. It has some reference value, for more information about how to download images in batches using Python, see the following example. Share it with you for your reference. The specific implementation method is as follows:

#! /Usr/bin/env python #-*-coding: UTF-8-*-'# Filename: download_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): content = urllib. urlopen (base_url ). read () array_url_a = re. findall (R'/rihan. *?. Html ', content) for url in array_url_a: url_a = 'xxx' + url # print url_a contains (url_a) def get_inner_url (array_url, inner_url): Contains (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 ). read () pic_url_a = re. findall (r'center. *?. Jpg ', content) print 'bbbbbbbbbb', 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.append (pic) j = j + 1 del pic_url [-1] for I in pic_url: print idef urlcallback (a, B, c): "" call back function a, downloaded data block B, data block size c, remote File Size "print" callback "prec = 100.0 * a * B/c if 100 <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) Partition (array_url, base_url) file_num = 3 # download (pic_url, file_num) Partition (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) encode (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 [:] '''

    I hope this article will help you with Python programming.

    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.