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 相关文章推荐
pydev使用wxpython找不到路径的解决方法
Feb 10 Python
scrapy自定义pipeline类实现将采集数据保存到mongodb的方法
Apr 16 Python
python 循环遍历字典元素的简单方法
Sep 11 Python
Python跨文件全局变量的实现方法示例
Dec 10 Python
Python使用try except处理程序异常的三种常用方法分析
Sep 05 Python
Python使用字典的嵌套功能详解
Feb 27 Python
对python 调用类属性的方法详解
Jul 02 Python
解决运行出现'dict' object has no attribute 'has_key'问题
Jul 15 Python
Pycharm编辑器功能之代码折叠效果的实现代码
Oct 15 Python
python某漫画app逆向
Mar 31 Python
详解解Django 多对多表关系的三种创建方式
Aug 23 Python
Python selenium绕过webdriver监测执行javascript
Apr 12 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许愿墙模块功能分析
2013/06/25 PHP
需要做特殊处理的DOM元素属性的访问
2010/11/05 Javascript
JS正则表达式验证数字代码
2014/01/28 Javascript
JavaScript 基本概念
2015/01/20 Javascript
javascript实现简单的鼠标拖动效果实例
2015/04/10 Javascript
有关JS中的0,null,undefined,[],{},'''''''',false之间的关系
2017/02/14 Javascript
vue.js如何将echarts封装为组件一键使用详解
2017/10/10 Javascript
no-vnc和node.js实现web远程桌面的完整步骤
2019/08/11 Javascript
vue路由切换之淡入淡出的简单实现
2019/10/31 Javascript
Vue中qs插件的使用详解
2020/02/07 Javascript
JavaScript检测浏览器是否支持CSS变量代码实例
2020/04/03 Javascript
python中django框架通过正则搜索页面上email地址的方法
2015/03/21 Python
利用Python和OpenCV库将URL转换为OpenCV格式的方法
2015/03/27 Python
利用QT写一个极简单的图形化Python闹钟程序
2015/04/07 Python
python中的闭包用法实例详解
2015/05/05 Python
python dict 字典 以及 赋值 引用的一些实例(详解)
2017/01/20 Python
python pandas中DataFrame类型数据操作函数的方法
2018/04/08 Python
kaggle+mnist实现手写字体识别
2018/07/26 Python
python GUI实现小球满屏乱跑效果
2019/05/09 Python
python使用 zip 同时迭代多个序列示例
2019/07/06 Python
Python使用matplotlib实现交换式图形显示功能示例
2019/09/06 Python
使用Keras加载含有自定义层或函数的模型操作
2020/06/10 Python
Python安装Bs4的多种方法
2020/11/28 Python
python 获取域名到期时间的方法步骤
2021/02/10 Python
会计专业推荐信
2013/10/29 职场文书
人力资源专业推荐信
2013/11/29 职场文书
土木工程专业个人求职信
2013/12/30 职场文书
我的求职择业计划书
2014/04/04 职场文书
大学生活动总结怎么写
2014/04/29 职场文书
党务公开方案
2014/05/06 职场文书
出差报告范文
2014/11/06 职场文书
财务总监岗位职责
2015/02/03 职场文书
道歉信怎么写
2015/05/12 职场文书
MySQL 数据类型详情
2021/11/11 MySQL
sql注入报错之注入原理实例解析
2022/06/10 MySQL
MySQL控制流函数(-if ,elseif,else,case...when)
2022/07/07 MySQL