python爬虫网页图片并保存到本地

#coding=utf-8
import urllib
import re
#py抓取页面图片并保存到本地

#获取页面信息
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html

#通过正则获取图片
def getImg(html):
reg = r'src="(.+?.jpg)" pic_ext'
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
return imglist
#循环把图片存到本地
x = 0
for imgurl in imglist:
#保存到本地
urllib.urlretrieve(imgurl,'/Applications/MAMP/image/%s.jpg' % x)
x+=1

html = getHtml("http://tieba.baidu.com/p/2460150866")

print getImg(html)

原文地址:https://www.cnblogs.com/yelinfeng/p/8384870.html