您的位置:首页 > 编程语言

20行代码实现网页图片抓取。(待完善

2016-04-16 11:38 513 查看
终于到这题了,话不多说。直接上代码。

#coding:utf-8
#By :晓明酱
#Date:2016/4/16
#参考:http://blog.csdn.net/xiaowanggedege/article/details/8650034
import urllib,re

def get_html(url):
page = urllib.urlopen(url)
html = page.read()
return html

def get_img(html):
reg = r'src="(.*?\.jpg)"'
imgre = re.compile(reg)                    #创建模式对象
imglist = re.findall(imgre, html)        #列出所有的匹配项
i = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl, r'D://img/%s.jpg'%i)
i+=1
html = get_html('http://tieba.baidu.com/p/4483145121')
print get_img(html)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: