-- coding: UTF-8 --
import re
import urllib
def getHtml(url):
page = urllib.urlopen(url) #打开一个url,page相当于url文件描述符
html = page.read() #读取web数据
return html #返回web数据
url="http://desk.zol.com.cn/"
html = getHtml(url)
print html
def getImg(html):
regexp = 'srch="(.+?.jpg)"' #html里的ipg的url
img = re.compile(regexp) #将正则字符串转换成正则对象
imglist = re.findall(img, html) #利用re.findall查出字符串中的所有匹配的字符串,返回的是匹配字符
串的列表
return imglist
def saveImg(imglist):
x=0 #将图片重命名下载
for imgurl in imglist:
urllib.urlretrieve(imgurl, '%d.jpg'%x) #利用urllib.urlretrieve(url,rename)将图片重命名下载,
x += 1
print getImg(html)
imglist = getImg(html)
saveImg(imglist)
网友评论