python抓取网页中的图片示例
程序员文章站
2023-11-30 20:31:40
复制代码 代码如下:#coding:utf8import reimport urllibdef gethtml(url): page =...
复制代码 代码如下:
#coding:utf8
import re
import urllib
def 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("//www.jb51.net")
getimg(html,'jpg')