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

优采云 发布时间: 2020-05-06 08:03

  本文实例述说了python爬虫实现获取动态gif格式恶搞图片的方式。分享给你们供你们参考,具体如下:

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

  本次爬取的网站是 居然搞笑网

  思路:

  获取当前页面内容

  查找页面中动图所代表的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

  动图存储位置:jimy/2017-3-16/搞笑动图/1/真是艰辛的选择。3f0fe8f6-09f8-11e7-9161-f8bc12753d1e.gif

  图片出处为:

  动图存储位置:jimy/2017-3-16/搞笑动图/1/这么贱会被砍死吧……3fa9da88-09f8-11e7-9161-f8bc12753d1e.gif

  图片出处为:

  动图存储位置:jimy/2017-3-16/搞笑动图/1/一看就是美国……4064e60c-09f8-11e7-9161-f8bc12753d1e.gif

  图片出处为:

  动图存储位置:jimy/2017-3-16/搞笑动图/1/新垣结衣的正经工作脸414b4f52-09f8-11e7-9161-f8bc12753d1e.gif

  图片出处为:

  动图存储位置:jimy/2017-3-16/搞笑动图/1/妹子这是在摇哪些的421afa86-09f8-11e7-9161-f8bc12753d1e.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程序设计有所帮助。

  如对本文有疑问,请在下边进行留言讨论,广大热心网友会与你互动!!

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线