Python crawler crawls web images

Source: Internet
Author: User

I did not think Python is so powerful, fascinating, previously saw the picture is always a copy and paste, now good, learn Python can use the program will be a picture, save it.

Today, I see a lot of beautiful pictures, but the picture a bit more, do not want to a copy and paste, how to do? There is always a way, even if there is no we can create a way.


Here's a look at the program I wrote today:

#Coding=utf-8#The urllib module provides an interface for reading Web page dataImportUrllib#the RE module mainly contains regular expressionsImportRe#define a gethtml () functiondefgethtml (URL): page= Urllib.urlopen (URL)#Urllib.urlopen () method to open a URL addresshtml = Page.read ()#the Read () method is used to read the data on the URL    returnHTMLdefgetimg (HTML): Reg= R'src= "(. +?\.jpg)" Pic_ext'    #regular expression, get the picture addressImgre = Re.compile (reg)#Re.compile () can compile a regular expression into a regular expression object.Imglist = Re.findall (imgre,html)#the Re.findall () method reads data in HTML that contains Imgre (regular expressions)    #Pass the filtered image address through the For loop and save to local    #The core is the Urllib.urlretrieve () method, which directly downloads the remote data to the local, and the image is incremented by x in turn namedx =0 forImgurlinchImglist:urllib.urlretrieve (Imgurl,'D:\E\%s.jpg'%x) x+=1HTML= Gethtml ("http://tieba.baidu.com/p/xxxx")PrintGetimg (HTML)

After running the program, here is the moment to witness the miracle, open the corresponding folder:


Wow!     The pictures are all saved, so nice! :-)

Python crawler crawls web images

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.