Python create a simple reptile, crawling to the picture you want

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

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:

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

Python provides a very powerful regular expressions, we need to first understand a little knowledge of python regular expression for the job.

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"

Modify the code as follows:

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:

If you meet a lot of questions and problems in the process of learning Python, you can add -qun 227 -435-450 there are software video for free

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:

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

通过一个for循环对获取的图片连接进行遍历,为了使图片的文件名看上去更规范,对其进行重命名,命名规则通过x变量加1。保存的位置默认为程序的存放目录。

程序运行完成,将在目录下看到下载到本地的文件。

做为一个过来人,我要告诉你请教前辈大牛真的很重要,可以让你少走很多的弯路,不要怕丢人,没面子,面子值几个钱?学到真本事才最重要。没有技能才叫真的没有面子。python技术分享,让你的前途不再迷茫。

推荐阅读:

零基础如何学习Python这门编程语言?

学习python12小时后,告诉你,学python真没你想的那么难!

编程学习,知识付费已是常事,知识变现更重要!

程序员的技能树,决定了一生职业的高度

更多互联网行业咨询,编程学习技能分享!关注公众号id: mtbcxx

Guess you like

Origin blog.csdn.net/weichen090909/article/details/90678616