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

 终于到这题了,话不多说。直接上代码。

#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)
原文地址:https://www.cnblogs.com/xaomng/p/5397991.html