Python爬虫实现获取动态gif格式搞笑图片的方法示例


Posted in Python onDecember 24, 2018

本文实例讲述了Python爬虫实现获取动态gif格式搞笑图片的方法。分享给大家供大家参考,具体如下:

有时候看到一些喜欢的动图,如果一个个取保存挺麻烦,有的网站还不支持右键保存,因此使用python来获取动态图,就看看就很有意思了

本次爬取的网站是  居然搞笑网 http://www.zbjuran.com/dongtai/list_4_1.html

思路:

获取当前页面内容

查找页面中动图所代表的url地址

保存这个地址内容到本地

如果想爬取多页,就可以加上一个循环条件

代码:

#!/usr/bin/python
#coding:utf-8
import urllib2,time,uuid,urllib,os,sys,re
from bs4 import BeautifulSoup
reload(sys)
sys.setdefaultencoding('utf-8')
#获取页面内容
def getHtml(url):
    try:
        print url
        html = urllib2.urlopen(url).read()#.decode('utf-8')#解码为utf-8
    except:
        return
    return html
#获取动图所代表的url列表
def getImagUrl(html):
    if not html:
        print 'nothing can be found'
        return
    ImagUrlList=[]
    soup=BeautifulSoup(html,'lxml')
    #获取item列表
    items=soup.find("div",{"class":"main"}).find_all('div',{'class':'item'})
    for item in items:
        target={}
        #通过if语句,过滤广告项
        if item.find('div',{"class":"text"}):
            #获取url
            imgurl=item.find('div',{"class":"text"}).find('img').get('src')
            target['url']=imgurl
            #获取名字
            target['name']=item.find('h3').text
            ImagUrlList.append(target)
    return ImagUrlList
#下载图片到本地
def download(author,imgurl,typename,pageNo):
    #定义文件夹的名字
    x = time.localtime(time.time())
    foldername = str(x.__getattribute__("tm_year"))+"-"+str(x.__getattribute__("tm_mon"))+"-"+str(x.__getattribute__("tm_mday"))
    download_img=None
    picpath = 'Jimy/%s/%s/%s' % (foldername,typename,str(pageNo))
    filename = author+str(uuid.uuid1())
    pic_type=imgurl[-3:]
    if not os.path.exists(picpath):
        os.makedirs(picpath)
    target = picpath+"/%s.%s" % (filename,pic_type)
    print "动图存贮位置:"+target
    download_img = urllib.urlretrieve(imgurl, target)#将图片下载到指定路径中
    print "图片出处为:"+imgurl
    return download_img
#退出函数
def myquit():
    print "Bye Bye!"
    exit(0)
def start(pageNo):
    targeturl="http://www.zbjuran.com/dongtai/list_4_%s.html" % str(pageNo)
    html = getHtml(targeturl)
    urllist=getImagUrl(html)
    for imgurl in urllist:
        download(imgurl['name'],imgurl['url'],'搞笑动图',pageNo)
if __name__ == '__main__':
    print '''
            *****************************************
            **  Welcome to Spider of GIF     **
            **   Created on 2017-3-16      **
            **   @author: Jimy         **
            *****************************************'''
    pageNo = raw_input("Input the page number you want to scratch (1-50),please input 'quit' if you want to quit\n\
请输入要爬取的页面,范围为(1-100),如果退出,请输入Q>\n>")
    while not pageNo.isdigit() or int(pageNo) > 50 or int(pageNo) < 1:
        if pageNo == 'Q':
            myquit()
        print "Param is invalid , please try again."
        pageNo = raw_input("Input the page number you want to scratch >")
    print pageNo
    start(pageNo)
    #第一次爬取结束
    pageNo = raw_input("Input the page number you want to scratch (1-50),please input 'quit' if you want to quit\n\
请输入总共需要爬取的页面,范围为(1-5000),如果退出,请输入Q>\n>")
    while not pageNo.isdigit() or int(pageNo) > 5000 or int(pageNo) < 1:
        if pageNo == 'Q':
            myquit()
        print "Param is invalid , please try again."
        pageNo = raw_input("Input the page number you want to scratch >")
    #循环遍历,爬取多页
    for num in xrange(int(pageNo)):
        start(str(num+1))

结果如下:

                        *****************************************
                        **    Welcome to Spider of GIF         **
                        **      Created on 2017-3-16           **
                        **      @author: Jimy                  **
                        *****************************************
Input the page number you want to scratch (1-50),please input 'quit' if you want to quit
请输入要爬取的页面,范围为(1-100),如果退出,请输入Q>
>1
1
http://www.zbjuran.com/dongtai/list_4_1.html
动图存贮位置:Jimy/2017-3-16/搞笑动图/1/真是艰难的选择。3f0fe8f6-09f8-11e7-9161-f8bc12753d1e.gif
图片出处为:http://www.zbjuran.com/uploads/allimg/170206/10-1F206135ZHJ.gif
动图存贮位置:Jimy/2017-3-16/搞笑动图/1/这么贱会被打死吧……3fa9da88-09f8-11e7-9161-f8bc12753d1e.gif
图片出处为:http://www.zbjuran.com/uploads/allimg/170206/10-1F206135H35U.gif
动图存贮位置:Jimy/2017-3-16/搞笑动图/1/一看就是印度……4064e60c-09f8-11e7-9161-f8bc12753d1e.gif
图片出处为:http://www.zbjuran.com/uploads/allimg/170206/10-1F20613543c50.gif
动图存贮位置:Jimy/2017-3-16/搞笑动图/1/新垣结衣的正经工作脸414b4f52-09f8-11e7-9161-f8bc12753d1e.gif
图片出处为:http://www.zbjuran.com/uploads/allimg/170206/10-1F206135250553.gif
动图存贮位置:Jimy/2017-3-16/搞笑动图/1/妹子这是在摇什么的421afa86-09f8-11e7-9161-f8bc12753d1e.gif
图片出处为:http://www.zbjuran.com/uploads/allimg/170206/10-1F20613493N03.gif
Input the page number you want to scratch (1-50),please input 'quit' if you want to quit
请输入总共需要爬取的页面,范围为(1-5000),如果退出,请输入Q>
>Q
Bye Bye!

最终就能够获得动态图了

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
python通过wxPython打开一个音频文件并播放的方法
Mar 25 Python
Python中time模块与datetime模块在使用中的不同之处
Nov 24 Python
itchat接口使用示例
Oct 23 Python
Python实现文件信息进行合并实例代码
Jan 17 Python
python如何为被装饰的函数保留元数据
Mar 21 Python
Python设计模式之解释器模式原理与用法实例分析
Jan 10 Python
Windows 安装 Anaconda3+PyCharm的方法步骤
Jun 13 Python
PyCharm中如何直接使用Anaconda已安装的库
May 28 Python
浅谈Python 命令行参数argparse写入图片路径操作
Jul 12 Python
Python同时处理多个异常的方法
Jul 28 Python
python实现暗通道去雾算法的示例
Sep 27 Python
python实战之90行代码写个猜数字游戏
Apr 22 Python
python 在屏幕上逐字显示一行字的实例
Dec 24 #Python
python之Flask实现简单登录功能的示例代码
Dec 24 #Python
python实现逐个读取txt字符并修改
Dec 24 #Python
Python判断一个list中是否包含另一个list全部元素的方法分析
Dec 24 #Python
python读取txt文件中特定位置字符的方法
Dec 24 #Python
python进行文件对比的方法
Dec 24 #Python
Python二叉树的遍历操作示例【前序遍历,中序遍历,后序遍历,层序遍历】
Dec 24 #Python
You might like
php实现的mysqldb读写分离操作类示例
2017/02/07 PHP
Laravel Validator自定义错误返回提示消息并在前端展示
2019/05/09 PHP
js querySelector和getElementById通过id获取元素的区别
2012/04/20 Javascript
jquery.blockUI.js上传滚动等待效果实现思路及代码
2013/03/18 Javascript
js实现两个值相加alert出来精确到指定位
2013/09/25 Javascript
Javascript仿新浪游戏频道鼠标悬停显示子菜单效果
2015/08/21 Javascript
分享一些常用的jQuery动画事件和动画函数
2015/11/27 Javascript
jQuery ready()和onload的加载耗时分析
2016/09/08 Javascript
详解js几个绕不开的事件兼容写法
2017/08/30 Javascript
基于打包工具Webpack进行项目开发实例
2018/05/29 Javascript
微信小程序获取音频时长与实时获取播放进度问题
2018/08/28 Javascript
使用mpvue搭建一个初始小程序及项目配置方法
2018/12/03 Javascript
微信小程序云开发之云函数详解
2019/05/16 Javascript
JavaScript静态作用域和动态作用域实例详解
2019/06/17 Javascript
vue radio单选框,获取当前项(每一项)的value值操作
2020/09/10 Javascript
[01:02]DOTA2上海特锦赛SHOWOPEN
2016/03/25 DOTA
python实现dict版图遍历示例
2014/02/19 Python
Python Web服务器Tornado使用小结
2014/05/06 Python
Python处理命令行参数模块optpars用法实例分析
2018/05/31 Python
在python环境下运用kafka对数据进行实时传输的方法
2018/12/27 Python
python爬取百度贴吧前1000页内容(requests库面向对象思想实现)
2019/08/10 Python
使用django和vue进行数据交互的方法步骤
2019/11/11 Python
基于jupyter代码无法在pycharm中运行的解决方法
2020/04/21 Python
Python ini文件常用操作方法解析
2020/04/26 Python
HTML5 canvas基本绘图之绘制阴影效果
2016/06/27 HTML / CSS
Belle Maison倍美丛官网:日本千趣会旗下邮购网站
2016/07/22 全球购物
阿根廷在线宠物商店:Puppis
2018/03/23 全球购物
办公室内勤岗位职责范本
2013/12/09 职场文书
就职演讲稿范文
2014/05/19 职场文书
城市创卫标语
2014/06/17 职场文书
2014年效能监察工作总结
2014/11/21 职场文书
村官个人总结范文
2015/03/03 职场文书
《地震中的父与子》教学反思
2016/02/16 职场文书
理解深度学习之深度学习简介
2021/04/14 Python
Redis批量生成数据的实现
2022/06/05 Redis
SQL中的连接查询详解
2022/06/21 SQL Server