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 分析Nginx访问日志并保存到MySQL数据库实例
Mar 13 Python
python两种遍历字典(dict)的方法比较
May 29 Python
python进阶教程之模块(module)介绍
Aug 30 Python
Python绘制3d螺旋曲线图实例代码
Dec 20 Python
Python 读取指定文件夹下的所有图像方法
Apr 27 Python
Python3.6中Twisted模块安装的问题与解决
Apr 15 Python
python解压TAR文件至指定文件夹的实例
Jun 10 Python
python pandas时序处理相关功能详解
Jul 03 Python
PyTorch中的padding(边缘填充)操作方式
Jan 03 Python
python GUI库图形界面开发之PyQt5拖放控件实例详解
Feb 25 Python
Python unittest框架操作实例解析
Apr 13 Python
刚学完怎么用Python实现定时任务,转头就跑去撩妹!
Jun 05 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排序算法之快速排序(Quick Sort)及其优化算法详解
2018/04/21 PHP
Jquery ajax传递复杂参数给WebService的实现代码
2011/08/08 Javascript
js有关元素内容操作小结
2011/12/20 Javascript
JS Jquery 遍历,筛选页面元素 自动完成(实现代码)
2013/07/08 Javascript
checkbox全选所涉及到的知识点介绍
2013/12/31 Javascript
JavaScript使用push方法添加一个元素到数组末尾用法实例
2015/04/06 Javascript
js获取form的方法
2015/05/06 Javascript
实现隔行换色效果的两种方式【实用】
2016/11/27 Javascript
深入理解js中的加载事件
2017/02/08 Javascript
关于javascript获取内联样式与嵌入式样式的实例
2017/06/01 Javascript
js学习总结之dom2级事件基础知识详解
2017/07/27 Javascript
php 解压zip压缩包内容到指定目录的实例
2018/01/23 Javascript
vue防止花括号{{}}闪烁v-text和v-html、v-cloak用法示例
2019/03/13 Javascript
微信小程序实现蓝牙打印
2019/09/23 Javascript
node.js实现简单的压缩/解压缩功能示例
2019/11/05 Javascript
Vue实例的对象参数options的几个常用选项详解
2019/11/08 Javascript
js实现轮播图特效
2020/05/28 Javascript
微信小程序完美解决scroll-view高度自适应问题的方法
2020/08/08 Javascript
JS实现简易日历效果
2021/01/25 Javascript
[59:35]DOTA2-DPC中国联赛定级赛 Aster vs DLG BO3第一场 1月8日
2021/03/11 DOTA
Python 中的 global 标识对变量作用域的影响
2019/08/12 Python
Numpy与Pytorch 矩阵操作方式
2019/12/27 Python
基于python实现计算且附带进度条代码实例
2020/03/31 Python
2020年10款优秀的Python第三方库,看看有你中意的吗?
2021/01/12 Python
Django中如何用xlwt生成表格的方法步骤
2021/01/31 Python
conda安装tensorflow和conda常用命令小结
2021/02/20 Python
新西兰珠宝品牌:Michael Hill
2017/09/16 全球购物
正宗的澳大利亚Ugg靴子零售商:UGG Express
2020/04/19 全球购物
Python面试题:Python是如何进行内存管理的
2014/08/04 面试题
小学教育毕业生自荐信
2013/11/18 职场文书
公司成立感言
2014/01/11 职场文书
房屋继承公证书
2014/04/10 职场文书
埃及王子观后感
2015/06/16 职场文书
文化大革命观后感
2015/06/17 职场文书
新店开业策划方案怎么书写?
2019/07/05 职场文书
解析MySQL binlog
2021/06/11 MySQL