欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python爬蟲(chóng)實(shí)現(xiàn)獲取動(dòng)態(tài)gif格式搞笑圖片的方法示例

 更新時(shí)間:2018年12月24日 09:46:56   作者:楓奇  
這篇文章主要介紹了Python爬蟲(chóng)實(shí)現(xiàn)獲取動(dòng)態(tài)gif格式搞笑圖片的方法,結(jié)合實(shí)例形式分析了Python針對(duì)gif格式圖片的爬取、下載等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Python爬蟲(chóng)實(shí)現(xiàn)獲取動(dòng)態(tài)gif格式搞笑圖片的方法。分享給大家供大家參考,具體如下:

有時(shí)候看到一些喜歡的動(dòng)圖,如果一個(gè)個(gè)取保存挺麻煩,有的網(wǎng)站還不支持右鍵保存,因此使用python來(lái)獲取動(dòng)態(tài)圖,就看看就很有意思了

本次爬取的網(wǎng)站是  居然搞笑網(wǎng) http://www.zbjuran.com/dongtai/list_4_1.html

思路:

獲取當(dāng)前頁(yè)面內(nèi)容

查找頁(yè)面中動(dòng)圖所代表的url地址

保存這個(gè)地址內(nèi)容到本地

如果想爬取多頁(yè),就可以加上一個(gè)循環(huán)條件

代碼:

#!/usr/bin/python
#coding:utf-8
import urllib2,time,uuid,urllib,os,sys,re
from bs4 import BeautifulSoup
reload(sys)
sys.setdefaultencoding('utf-8')
#獲取頁(yè)面內(nèi)容
def getHtml(url):
    try:
        print url
        html = urllib2.urlopen(url).read()#.decode('utf-8')#解碼為utf-8
    except:
        return
    return html
#獲取動(dòng)圖所代表的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={}
        #通過(guò)if語(yǔ)句,過(guò)濾廣告項(xiàng)
        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 "動(dòng)圖存貯位置:"+target
    download_img = urllib.urlretrieve(imgurl, target)#將圖片下載到指定路徑中
    print "圖片出處為:"+imgurl
    return download_img
#退出函數(shù)
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'],'搞笑動(dòng)圖',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\
請(qǐng)輸入要爬取的頁(yè)面,范圍為(1-100),如果退出,請(qǐng)輸入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)
    #第一次爬取結(jié)束
    pageNo = raw_input("Input the page number you want to scratch (1-50),please input 'quit' if you want to quit\n\
請(qǐng)輸入總共需要爬取的頁(yè)面,范圍為(1-5000),如果退出,請(qǐng)輸入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 >")
    #循環(huán)遍歷,爬取多頁(yè)
    for num in xrange(int(pageNo)):
        start(str(num+1))

結(jié)果如下:

                        *****************************************
                        **    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
請(qǐng)輸入要爬取的頁(yè)面,范圍為(1-100),如果退出,請(qǐng)輸入Q>
>1
1
http://www.zbjuran.com/dongtai/list_4_1.html
動(dòng)圖存貯位置:Jimy/2017-3-16/搞笑動(dòng)圖/1/真是艱難的選擇。3f0fe8f6-09f8-11e7-9161-f8bc12753d1e.gif
圖片出處為:http://www.zbjuran.com/uploads/allimg/170206/10-1F206135ZHJ.gif
動(dòng)圖存貯位置:Jimy/2017-3-16/搞笑動(dòng)圖/1/這么會(huì)被打死吧……3fa9da88-09f8-11e7-9161-f8bc12753d1e.gif
圖片出處為:http://www.zbjuran.com/uploads/allimg/170206/10-1F206135H35U.gif
動(dòng)圖存貯位置:Jimy/2017-3-16/搞笑動(dòng)圖/1/一看就是印度……4064e60c-09f8-11e7-9161-f8bc12753d1e.gif
圖片出處為:http://www.zbjuran.com/uploads/allimg/170206/10-1F20613543c50.gif
動(dòng)圖存貯位置:Jimy/2017-3-16/搞笑動(dòng)圖/1/新垣結(jié)衣的正經(jīng)工作臉414b4f52-09f8-11e7-9161-f8bc12753d1e.gif
圖片出處為:http://www.zbjuran.com/uploads/allimg/170206/10-1F206135250553.gif
動(dòng)圖存貯位置:Jimy/2017-3-16/搞笑動(dòng)圖/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
請(qǐng)輸入總共需要爬取的頁(yè)面,范圍為(1-5000),如果退出,請(qǐng)輸入Q>
>Q
Bye Bye!

最終就能夠獲得動(dòng)態(tài)圖了

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • python中numpy的矩陣、多維數(shù)組的用法

    python中numpy的矩陣、多維數(shù)組的用法

    本篇文章主要介紹了python中numpy的矩陣、多維數(shù)組的用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • 最新評(píng)論