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

 更新时间:2014年04月27日 10:45:33   作者:  
这篇文章主要介绍了python抓取网页图片示例(python爬虫),需要的朋友可以参考下

复制代码 代码如下:

#-*- 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."

相关文章

  • 开源软件包和环境管理系统Anaconda的安装使用

    开源软件包和环境管理系统Anaconda的安装使用

    Anaconda是一个用于科学计算的Python发行版,支持 Linux, Mac, Windows系统,提供了包管理与环境管理的功能,可以很方便地解决多版本python并存、切换以及各种第三方包安装问题。
    2017-09-09
  • 使用tensorflow实现AlexNet

    使用tensorflow实现AlexNet

    这篇文章主要为大家详细介绍了使用tensorflow实现AlexNet,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • Python用HBuilder创建交流社区APP

    Python用HBuilder创建交流社区APP

    这篇文章主要讲解Python使用HBuilder创建交流社区APP,使用HBuilder做一个简单的社区浏览界面,下面文章附有详细的代码,需要的朋友可以参考一下
    2021-11-11
  • python实现上传文件到linux指定目录的方法

    python实现上传文件到linux指定目录的方法

    这篇文章主要介绍了python实现上传文件到linux指定目录的方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-01-01
  • 基于Flask实现文件上传七牛云中并下载

    基于Flask实现文件上传七牛云中并下载

    文件上传是Web应用中常见的功能之一,而七牛云则提供了强大的云存储服务,本文我们将学习如何在Flask应用中实现文件上传,并将上传的文件保存到七牛云,感兴趣的可以学习一下
    2023-10-10
  • python中根据字符串调用函数的实现方法

    python中根据字符串调用函数的实现方法

    下面小编就为大家带来一篇python中根据字符串调用函数的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考,一起跟随小编过来看看吧
    2016-06-06
  • Python读写ini文件的方法

    Python读写ini文件的方法

    这篇文章主要介绍了Python读写ini文件的方法,实例分析了Python针对ini配置文件的读写及修改等操作技巧,需要的朋友可以参考下
    2015-05-05
  • Matplotlib自定义坐标轴刻度的实现示例

    Matplotlib自定义坐标轴刻度的实现示例

    这篇文章主要介绍了Matplotlib自定义坐标轴刻度的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • Python3中FuzzyWuzzy库实例用法

    Python3中FuzzyWuzzy库实例用法

    在本篇文章中小编给各位整理了关于Python3z中FuzzyWuzzy库实例用法及相关代码,有兴趣的朋友们可以参考下。
    2020-11-11
  • Python使用while循环花式打印乘法表

    Python使用while循环花式打印乘法表

    今天小编就为大家分享一篇关于Python使用while循环花式打印乘法表,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01

最新评论