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