Python爬蟲(chóng)實(shí)現(xiàn)獲取動(dòng)態(tài)gif格式搞笑圖片的方法示例
本文實(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ò)python爬蟲(chóng)賺錢的方法
- Python爬蟲(chóng)實(shí)戰(zhàn)之12306搶票開(kāi)源
- Python反爬蟲(chóng)技術(shù)之防止IP地址被封殺的講解
- python爬蟲(chóng)獲取小區(qū)經(jīng)緯度以及結(jié)構(gòu)化地址
- Python3爬蟲(chóng)之urllib攜帶cookie爬取網(wǎng)頁(yè)的方法
- python3爬蟲(chóng)怎樣構(gòu)建請(qǐng)求header
- Python爬蟲(chóng)文件下載圖文教程
- Python使用Chrome插件實(shí)現(xiàn)爬蟲(chóng)過(guò)程圖解
相關(guān)文章
Python退出時(shí)強(qiáng)制運(yùn)行一段代碼的實(shí)現(xiàn)方法
這篇文章主要介紹了Python退出時(shí)強(qiáng)制運(yùn)行一段代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04如何用 Python 子進(jìn)程關(guān)閉 Excel 自動(dòng)化中的彈窗
這篇文章主要介紹了如何用 Python 子進(jìn)程關(guān)閉 Excel 自動(dòng)化中的彈窗,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-05-05minpy使用GPU加速Numpy科學(xué)計(jì)算方式
這篇文章主要介紹了minpy使用GPU加速Numpy科學(xué)計(jì)算方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01python 讀取文件并把矩陣轉(zhuǎn)成numpy的兩種方法
今天小編就為大家分享一篇python 讀取文件并把矩陣轉(zhuǎn)成numpy的兩種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02Python venv虛擬環(huán)境配置過(guò)程解析
這篇文章主要介紹了Python venv虛擬環(huán)境配置過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07python解決漢字編碼問(wèn)題:Unicode Decode Error
最近在利用python讀取一個(gè)含有漢字的文檔時(shí)導(dǎo)致出現(xiàn)了亂碼,并報(bào)出了兩個(gè)錯(cuò)誤,無(wú)奈只能上網(wǎng)找尋答案,后通過(guò)網(wǎng)友的幫助解決了這個(gè)問(wèn)題,想著總結(jié)一下,下面這篇文章就主要介紹了python如何解決漢字編碼問(wèn)題,有需要的朋友們可以參考借鑒。2017-01-01