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 不同对象比较大小示例探讨
Aug 21 Python
在Python中操作列表之List.append()方法的使用
May 20 Python
Python ValueError: invalid literal for int() with base 10 实用解决方法
Jun 21 Python
浅谈python中的__init__、__new__和__call__方法
Jul 18 Python
Python数据分析中Groupby用法之通过字典或Series进行分组的实例
Dec 08 Python
Python利用heapq实现一个优先级队列的方法
Feb 03 Python
利用anaconda保证64位和32位的python共存
Mar 09 Python
Python3 mmap内存映射文件示例解析
Mar 23 Python
django使用JWT保存用户登录信息
Apr 22 Python
Pytest测试框架基本使用方法详解
Nov 25 Python
python实现简单的名片管理系统
Apr 26 Python
详解Golang如何实现支持随机删除元素的堆
Sep 23 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
一个简单的域名注册情况查询程序
2006/10/09 PHP
PHP实现表单提交数据的验证处理功能【防SQL注入和XSS攻击等】
2017/07/21 PHP
PHP生成腾讯云COS接口需要的请求签名
2018/05/20 PHP
【消息提示组件】,兼容IE6/7&amp;&amp;FF2
2007/09/04 Javascript
javascript 弹出窗口中是否显示地址栏的实现代码
2011/04/14 Javascript
javascript中拼接HTML字符串的最快、最好的方法
2014/06/07 Javascript
js简单抽奖代码
2015/01/16 Javascript
JavaScript设置表单上传时文件个数的方法
2015/08/11 Javascript
Javascript实现检测客户端类型代码封包
2015/12/03 Javascript
jQuery表单验证插件解析(推荐)
2016/07/21 Javascript
js入门之Function函数的使用方法【新手必看】
2016/11/22 Javascript
BootStrap便签页的简单应用
2017/01/06 Javascript
JS实现针对给定时间的倒计时功能示例
2017/04/11 Javascript
详解windows下vue-cli及webpack 构建网站(三)使用组件
2017/06/17 Javascript
详解AngularJS1.x学习directive 中‘&amp; ’‘=’ ‘@’符号的区别使用
2017/08/23 Javascript
jQuery实现的简单动态添加、删除表格功能示例
2017/09/21 jQuery
node.js自动上传ftp的脚本分享
2018/06/16 Javascript
vue实现简单学生信息管理
2020/05/30 Javascript
详解vue实现坐标拾取器功能示例
2020/11/18 Vue.js
Python数据结构与算法之图的基本实现及迭代器实例详解
2017/12/12 Python
python使用循环打印所有三位数水仙花数的实例
2018/11/13 Python
python多线程与多进程及其区别详解
2019/08/08 Python
基于python实现把图片转换成素描
2019/11/13 Python
win10下python2和python3共存问题解决方法
2019/12/23 Python
如何使用python代码操作git代码
2020/02/29 Python
如何在python中执行另一个py文件
2020/04/30 Python
python字典按照value排序方法
2020/12/28 Python
python 模块导入问题汇总
2021/02/01 Python
python time.strptime格式化实例详解
2021/02/03 Python
H5离线存储Manifest原理及使用
2020/04/28 HTML / CSS
超市营业员岗位职责
2013/12/20 职场文书
学生不讲诚信检讨书
2014/09/29 职场文书
离婚协议书范本2014
2014/10/27 职场文书
处级干部考察材料
2014/12/24 职场文书
家电创业计划书
2019/08/05 职场文书
python opencv检测直线 cv2.HoughLinesP的实现
2021/06/18 Python