python抓取网页中的图片示例

代码如下:

#coding:utf8import reimport urllibdef gethtml(url): page = urllib.urlopen(url) html = page.read() return html

def getimg(html,imgtype): reg = r’src=”(.*?\.+’+imgtype+’!slider)” ‘ imgre = re.compile(reg) imglist = re.findall(imgre, html) x=0 for imgurl in imglist: print imgurl urllib.urlretrieve(imgurl, ‘%s.%s’ % (x, imgtype)) x =x+1

html= gethtml(“http://www.jb51.net”)

getimg(html,’jpg’)

发表评论