python抓取网页图片示例(python爬虫)


Posted in Python onApril 27, 2014
#-*- encoding: utf-8 -*-
'''
Created on 2014-4-24
@author: Leon Wong
'''
import urllib2
import urllib
import re
import time
import os
import uuid
#获取二级页面url
def findUrl2(html):
    re1 = r'http://tuchong.com/\d+/\d+/|http://\w+(?<!photos).tuchong.com/\d+/'
    url2list = re.findall(re1,html)
    url2lstfltr = list(set(url2list))
    url2lstfltr.sort(key=url2list.index)
    #print url2lstfltr
    return url2lstfltr
#获取html文本
def getHtml(url):
    html = urllib2.urlopen(url).read().decode('utf-8')#解码为utf-8
    return html
#下载图片到本地
def download(html_page , pageNo):   
    #定义文件夹的名字
    x = time.localtime(time.time())
    foldername = str(x.__getattribute__("tm_year"))+"-"+str(x.__getattribute__("tm_mon"))+"-"+str(x.__getattribute__("tm_mday"))
    re2=r'http://photos.tuchong.com/.+/f/.+\.jpg'
    imglist=re.findall(re2,html_page)
    print imglist
    download_img=None
    for imgurl in imglist:
        picpath = 'D:\\TuChong\\%s\\%s'  % (foldername,str(pageNo))
        filename = str(uuid.uuid1())
        if not os.path.exists(picpath):
            os.makedirs(picpath)               
        target = picpath+"\\%s.jpg" % filename
        print "The photos location is:"+target
        download_img = urllib.urlretrieve(imgurl, target)#将图片下载到指定路径中
        time.sleep(1)
        print(imgurl)
    return download_img

# def callback(blocknum, blocksize, totalsize):
#     '''回调函数
#     @blocknum: 已经下载的数据块
#     @blocksize: 数据块的大小
#     @totalsize: 远程文件的大小
#     '''
#     print str(blocknum),str(blocksize),str(totalsize)
#     if blocknum * blocksize >= totalsize:
#         print '下载完成'
def quitit():
    print "Bye!"
    exit(0)
    
if __name__ == '__main__':
    print '''            *****************************************
            **    Welcome to Spider for TUCHONG    **
            **      Created on 2014-4-24           **
            **      @author: Leon Wong             **
            *****************************************'''
    pageNo = raw_input("Input the page number you want to scratch (1-100),please input 'quit' if you want to quit>")
    while not pageNo.isdigit() or int(pageNo) > 100 :
        if pageNo == 'quit':quitit()
        print "Param is invalid , please try again."
        pageNo = raw_input("Input the page number you want to scratch >")
    #针对图虫人像模块来爬取
    html = getHtml("http://tuchong.com/tags/%E4%BA%BA%E5%83%8F/?page="+str(pageNo))
    detllst = findUrl2(html)
    for detail in detllst:
        html2 = getHtml(detail)
        download(html2,pageNo)
    print "Finished."
Python 相关文章推荐
python文件和目录操作方法大全(含实例)
Mar 12 Python
windows系统中python使用rar命令压缩多个文件夹示例
May 06 Python
python模块之StringIO使用示例
Apr 08 Python
详解Python当中的字符串和编码
Apr 25 Python
用Python实现斐波那契(Fibonacci)函数
Mar 25 Python
Flask框架中密码的加盐哈希加密和验证功能的用法详解
Jun 07 Python
[原创]pip和pygal的安装实例教程
Dec 07 Python
Python常见字典内建函数用法示例
May 14 Python
将python运行结果保存至本地文件中的示例讲解
Jul 11 Python
Python测试模块doctest使用解析
Aug 10 Python
Python Subprocess模块原理及实例
Aug 26 Python
python实现批处理文件
Jul 28 Python
python实现sublime3的less编译插件示例
Apr 27 #Python
python中的实例方法、静态方法、类方法、类变量和实例变量浅析
Apr 26 #Python
Python设计模式之单例模式实例
Apr 26 #Python
Python设计模式之观察者模式实例
Apr 26 #Python
Python设计模式之代理模式实例
Apr 26 #Python
python中的列表推导浅析
Apr 26 #Python
Python中的Numpy入门教程
Apr 26 #Python
You might like
PHP代码保护--Zend Guard的使用详解
2013/06/03 PHP
php日历制作代码分享
2014/01/20 PHP
PHP封装的一个支持HTML、JS、PHP重定向的多功能跳转函数
2014/06/19 PHP
PHP实现把文本中的URL转换为链接的auolink()函数分享
2014/07/29 PHP
ThinkPHP中Widget扩展的两种写法及调用方法详解
2017/05/04 PHP
使用javascript访问XML数据的实例
2006/12/27 Javascript
javascript中将Object转换为String函数代码 (json str)
2012/04/29 Javascript
基于jQuery实现鼠标点击导航菜单水波动画效果附源码下载
2016/01/06 Javascript
AngularJS前端页面操作之用户修改密码功能示例
2017/03/27 Javascript
vue如何使用 Slot 分发内容实例详解
2017/09/05 Javascript
让你5分钟掌握9个JavaScript小技巧
2018/06/09 Javascript
react同构实践之实现自己的同构模板
2019/03/13 Javascript
微信小程序跳转到其他网页(外部链接)的实现方法
2019/09/20 Javascript
JS Ajax请求会话过期处理问题解决方法分析
2019/11/16 Javascript
ES5和ES6中类的区别总结
2020/12/21 Javascript
浅析python 中__name__ = '__main__' 的作用
2014/07/05 Python
Python 执行字符串表达式函数(eval exec execfile)
2014/08/11 Python
简单使用Python自动生成文章
2014/12/25 Python
Python实现邮件的批量发送的示例代码
2018/01/23 Python
Python读写/追加excel文件Demo分享
2018/05/03 Python
Django生成PDF文档显示在网页上以及解决PDF中文显示乱码的问题
2019/07/04 Python
Python装饰器使用你可能不知道的几种姿势
2019/10/25 Python
django 框架实现的用户注册、登录、退出功能示例
2019/11/28 Python
对tensorflow中cifar-10文档的Read操作详解
2020/02/10 Python
详解用Python调用百度地图正/逆地理编码API
2020/07/02 Python
用python计算文件的MD5值
2020/12/23 Python
使用html2canvas实现浏览器截图的示例代码
2018/01/26 HTML / CSS
大学共青团员个人自我评价
2014/04/16 职场文书
教师演讲稿大全
2014/05/16 职场文书
2014年中学生检讨书大全
2014/10/09 职场文书
2014大学生学生会工作总结
2014/12/19 职场文书
公司慰问信范文
2015/03/23 职场文书
Python一行代码实现自动发邮件功能
2021/05/30 Python
Python机器学习之决策树和随机森林
2021/07/15 Javascript
JavaScript中isPrototypeOf函数
2021/11/07 Javascript
vue使用echarts实现折线图
2022/03/21 Vue.js