Python write Web Crawler

Source: Internet
Author: User

This article code from the valley education Yan Qi fresh teacher video, address http://www.csvt.net/resource/video/6844/largesize

Note: This article aims to practice the simple usage of regular expressions.

#! /Usr/bin/evn python

#-*-Coding: cp936 -*-

Import re # import the regular expression Module
Import urllib # import the urllib module, which is used to read and download pages.

Def getHtml (url): # defines the getHtml () function to obtain the page source code.
Page = urllib. urlopen (url) # urlopen () obtains the page source code based on the url.
Html = page. read () # read content from the retrieved object
Return html

Def getImage (html): # defines the getImage () function to get the image address and download it.
Reg = r 'src = \' # \ '"width' # define a regular expression that matches the url of an image address
Imgre = re. compile (reg) # compile regular expressions to Improve Running Efficiency
Imagelist = imgre. findall (html) # Use findall () to find the image url matching the Regular Expression in html
X = 0
For imageurl in imagelist:
Urllib.urlretrieve(imageurl,'picture_%s.jpg '% x) # urlretrieve () download an object
X + = 1

Uri = raw_input ("Enter the URL :")
R = R' ^ http ://'
If re. match (r, uri ):
Html2 = getHtml (uri)
Else:
Html2 = getHtml ("http: //" + uri)
GetImage (html2)

Run the Script test. py

650) this. width = 650; "style =" width: 547px; height: 331px "border =" 0 "alt =" "width =" 519 "height =" 345 "src =" http://www.bkjia.com/uploads/allimg/131228/1151444E0-0.jpg "/>

The captured image is as follows:

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1151442K9-1.jpg "/>

 

 

This article from the "Zhao Haihua _ O & M Road" blog, please be sure to retain this source http://baiying.blog.51cto.com/1068039/1169299

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.