当前位置: 编程技术>python
python抓取网页中的图片示例
来源: 互联网 发布时间:2014-10-04
本文导语: 代码如下:#coding:utf8import reimport urllibdef getHTML(url): page = urllib.urlopen(url) html = page.read() return html def getImg(html,imgType): reg = r'src="/tech-python/(/.._'+imgType+'!slider)" ' imgre = re.compile(reg) imgList = re.findall(imgre, html) x=0 ...
代码如下:
#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="/tech-python/(/.._'+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.")
getImg(html,'jpg')