您的位置:首页 > 编程语言 > Python开发

Python 获取sina首页所有jpg图片

2014-09-06 22:00 393 查看
#!/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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: