Python implements a simple crawler

Source: Internet
Author: User

Now the online resources are particularly rich, especially if you see some of the site's good-looking pictures will want to save, but it is troublesome to need one click to download, but if we use the program to deal with, the problem will become very simple, only need to run the program to get all the pictures on the entire page.

Below we can look at how to use Python to achieve a simple Web page image to get the crawler applet.

First, how to get the whole page?

getpage.py

Import urllibdef getpage (URL):    page = urllib.urlopen (URL)    content = Page.read ()    return contentcontent = GetPage ("http://tieba.baidu.com/p/2510089409")

The first thing to do is import the Urllib module, which helps us to open any resource through the URL

Urlopen (URL, [, data])--To open a Web page according to the URL, according to the parameters to distinguish the post or get

Read (): Read all information on the entire page

Second, how to get only pictures?

savephotos.py

Import urllibimport redef getpage (URL):    page = urllib.urlopen (URL)    content = Page.read ()    return contentdef Getphotos (content):    reg = R ' src= "(. +?\.jpg)" Pic_ext '    Imgre = Re.compile (reg)    imglist = Re.findall (Imgre, Content)    x = 0    for Imgurl in imglist:        urllib.urlretrieve (Imgurl, './photos/%s.jpg '% x)        x+=1content = GetPage ("http://tieba.baidu.com/p/2510089409") print Getphotos (content)

The regular expression. Here we use regular expressions to filter the information we get to the Web page.

Import Urllib and re modules

Parsing a statement in a method getphotos.py

The first step: Create a regular expression filter rule;

Step two: Compile the regular expression

Step three: Save the rule-compliant file as a picture in imglist

Fourth step: Save the picture locally using the Urlretrieve () method. Where the second parameter setting saves the file location.

This makes it possible to implement a Python simple crawler that gets web images.

Climb to the picture, as follows:

 

Python implements a simple crawler

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.