首页 > 其他 > 详细

简单爬虫-爬贴吧图片

时间:2017-04-08 23:45:18      阅读:325      评论:0      收藏:0      [点我收藏+]
#coding=utf-8

#urllib模块提供了读取Web页面数据的接口
import urllib
#re模块主要包含了正则表达式
import re
#定义一个getHtml()函数
def getHtml(url):
    page = urllib.urlopen(url)  #urllib.urlopen()方法用于打开一个URL地址
    html = page.read() #read()方法用于读取URL上的数据
    return html

def getImg(html):
    reg = rsrc="(.+?\.jpg)" pic_ext    #正则表达式,得到图片地址
    imgre = re.compile(reg)     #re.compile() 可以把正则表达式编译成一个正则表达式对象.
    imglist = re.findall(imgre,html)      #re.findall() 方法读取html 中包含 imgre(正则表达式)的    数据
    #把筛选的图片地址通过for循环遍历并保存到本地
    #核心是urllib.urlretrieve()方法,直接将远程数据下载到本地,图片通过x依次递增命名
    x = 0

    for imgurl in imglist:
    urllib.urlretrieve(imgurl,D:\E\%s.jpg % x)
            x+=1


html = getHtml("http://tieba.baidu.com/p/xxxx")
print getImg(html)

 

简单爬虫-爬贴吧图片

原文:http://www.cnblogs.com/wangtao1993/p/6683475.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!