#!/usr/bin/python
#encoding:utf8
import re #导入正则库
import urllib
def getHtmlSource( url ): #获取网页源代码
pageHandle = urllib.urlopen( url )
htmlSource = pageHandle.read()
return htmlSource
def getImgHref( source ): #获取所有的链接
reg = r'src="(.*?\.jpg)" width'
imageRe = re.compile(reg)
imageList = re.findall(imageRe, source)
return imageList
def downLoad(imageList): #进行下载
count = 0
for imageUrl in imageList:
print "download ",imageUrl," now!"
urllib.urlretrieve(imageUrl, "%s.jpg" % count) #下载 并更改名称
count += 1
source = getHtmlSource(" http://www.sina.com.cn/")
imageList = getImgHref( source )
downLoad(imageList)