python reptile simple function

In our daily Internet browsing, we often see some nice pictures, we want to save these images to download, desktop wallpaper or the user used to do, or to do the design of the material.

  Our most common approach is through the right mouse button and select Save As. But some pictures of mouse right time and not the Save As option, there is a way to pass down through the interception screenshot tool, but that would reduce the clarity of the picture. Well ~! In fact, you are very powerful, right view the page source code.

We can achieve this functionality through a simple reptile python, the code we want crawled locally. Here's a look at how to use python to implement such a feature.

First, access to the entire page data

First, we can first obtain information about the entire page to download the picture.

getjpg.py

13406307-e570fa0987998d18.png

Urllib module provides a data interface to read web pages, we can read the data on the www and ftp like to read a local file. First, we define a getHtml () function:

urllib.urlopen () method is used to open a URL address.

read () method for reading data from the URL, to the getHtml () function is passed a web site and download the entire page. The implementation of the program will cover the whole page printout.

Second, the screening of the desired data page

If we Baidu Post Bar found a few nice wallpaper, viewer through the front section. I found a picture of address, such as: src = "http://imgsrc.baidu.com/forum......jpg" pic_ext = "jpeg"

13406307-9cb1335e98e0d5e6.png

Modify the code as follows:

13406307-d8d1538be45fd2db.png

We have created a getImg () function is used to screen the entire page for a picture of the need to connect. re module mainly contains the regular expression:

  re.compile () can be compiled into a regular expression regular expression object.

the re.findall () method of reading data contained in the html imgre (regular expression).

Run the script URL address of the entire page will be included in the picture.

Third, save the page filter data to the local

The address for the screening of the picture and save it to the local loop through the code as follows:

13406307-d6d5ed9e692d2a33.png

The core here is to use the urllib.urlretrieve () method, direct download remote data locally.

Get the picture connection to traverse through a for loop, in order to make the picture look more standardized file name, rename, naming rules by adding variable x 1. The default save location for storing the program directory.

Program finishes running, you will see the file downloaded to the local directory.

13406307-d811d8ed8d956c11.png

Guess you like

Origin blog.csdn.net/weixin_34061482/article/details/91011003