Teach you how to use Python to download CL community images automatically

Source: Internet
Author: User

Requirements Description:

Recently found a lot of the essence of the CL community posts to share the picture is very good, good to want to save, but one to save too much time, so the creator of the ideological I would like to write a tool to achieve as long as the link to enter the post, you can download all the essence of the picture.

Program Analysis Ideas:

Python is a great tool that is very handy to use. So I decided to use Python to implement it.

The idea is probably, I first enter the link, then each picture link, then go to download it. The demand is clear, the logic should be clear, start to do it.

Environment Description: I am using the Win7+python3

First import re, requests, OS three packages for crawlers, OS packages for directory creation

Enter the link URL of the image you want to save = input ("Input key URL:")

Here take http://dd.dety.men/htm_mob/16/1808/32xxxxx.html as an example, open in the browser is like this, because cannot be described, I can only put such a thing

Here to get the content of the Web page result = Requests.get (URL)

Result.text is to get to the source of the Web page, in order to avoid the problem of coding problems, we now do a bit of transcoding before

Then we need to find the title in the source code, one will be used as a file name to save the file, the title of the search implementation code is, so that the return is a list, we use the time to take a No. 0 directly (the program ape number is zero-based).

Here is to say the search for the title of the implementation code of the regular expression, we see the link in the Web source is

The regular expression that implements the code is to find the middle part of <title> and </title>, and find the title as follows

Below to do business, from the Web page to find the link to the picture, first look at the browser, press F12 to open the developer mode, click "Select an element from the page", select the picture, see the source of the image

<input data-src= "Http://www.79img.com/u/20180829/13435782.jpg"

But we need to look at Python to see what the source of the re gets,

Get to the picture link is like this data-src=\ ' http://www.79img.com/u/20180829/13392462.jpg\ ', so we use the following code to find all the picture links

Pic_url = Re.findall (' data-src=\ ' (. *?) \ ', Result_text, re. S

After finding the link is downloaded, this is the method of download, keyword is the title name just found

Added an operation to create a directory

Source code and how to use:

The final source code is as follows:

Source has been uploaded to GitHub, welcome to download, welcome all kinds of star,fork

Https://github.com/rootzhongfengshan/python_practical/tree/master/DownPictureFromCL

by use

You can see in the current directory produced a directory, show you the final chart of the results, very yellow and violent.

Teach you how to use Python to download CL community images automatically

Related Article

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.