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