使用Python实现网站图片抓取

1,Python环境

使用Python实现网站图片抓取

2,下面直接编写代码

import urllib
import re
import time

def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
html = html.decode('utf-8')
return html

def getImgUrl(html):
str_re = r'<img[^>]src[=\"\']+([^\"\'])[\"\'][^>]*>'
imgre = re.compile(str_re,re.I)
imglist = re.findall(imgre,html)
for i in imglist:
urllib.urlretrieve(i,'/mnt/test/'+str(time.time())+".jpg")

#last setup is invocation and test it

html = getHtml('http://xxx.xxx.xxx')
getImgUrl(html)

猜你喜欢

转载自blog.51cto.com/13718210/2134963