利用python爬取斗魚app中照片方法實例
前言
沒想到python是如此強大,令人著迷,以前看見圖片總是一張一張復(fù)制粘貼,現(xiàn)在好了,學(xué)會python就可以用程序?qū)⒁粡垙垐D片,保存下來。
最近看到斗魚里的照片都不錯,決定用最新學(xué)習(xí)的python技術(shù)進行爬取,下面將實現(xiàn)的過程分享出來供大家參考,下面話不多說了,來一起看看詳細的介紹吧。
方法如下:
首先下載一個斗魚(不下載也可以,url都在這了對吧)
通過抓包,抓取到一個json的數(shù)據(jù)包,得到下面的地址
觀察測試可知,通過修改offset值就是相當(dāng)于app的翻頁
訪問這個url,返回得到的是一個大字典,字典里面兩個索引,一個error,一個data。而data又是一個長度為20的數(shù)組,每個數(shù)組又是一個字典。每個字典中又有一個索引,vertical_src。
我們的目標就是它了!
import urllib.parse import urllib import json import urllib.request data_info={} data_info['type']='AUTO' data_info['doctype']='json' data_info['xmlVersion']='1.6' data_info['ue']='UTF-8' data_info['typoResult']='true' head_info={} head_info['User-Agent']='DYZB/2.271 (iphone; iOS 9.3.2; Scale/3.00)' url='http://capi.douyucdn.cn/api/v1/getVerticalRoom?aid=ios&client_sys=ios&limit=20&offset=20' data_info=urllib.parse.urlencode(data_info).encode('utf-8') print(data_info) requ=urllib.request.Request(url,data_info) requ.add_header('Referer','http://capi.douyucdn.cn') requ.add_header('User-Agent','DYZB/2.271 (iphone; iOS 9.3.2; Scale/3.00)') response=urllib.request.urlopen(requ) print(response) html=response.read().decode('utf-8')
這短短20多行代碼就能返回得到j(luò)son數(shù)據(jù)了。然后再通過對這json代碼的切片,分離得到每個主播照片的url地址。
然后得到這一頁的照片
import json import urllib.request data_info={} data_info['type']='AUTO' data_info['doctype']='json' data_info['xmlVersion']='1.6' data_info['ue']='UTF-8' data_info['typoResult']='true' url+str(i)='http://capi.douyucdn.cn/api/v1/getVerticalRoom?aid=ios&client_sys=ios&limit=20&offset='+str(x) data_info=urllib.parse.urlencode(data_info).encode('utf-8') print(data_info) requ=urllib.request.Request(url,data_info) requ.add_header('Referer','http://capi.douyucdn.cn') requ.add_header('User-Agent','DYZB/2.271 (iphone; iOS 9.3.2; Scale/3.00)') response=urllib.request.urlopen(requ) print(response) html=response.read().decode('utf-8') ''' print(type(dictionary)) print(type(dictionary[data])) ''' dictionary=json.loads(html) data_arr=dictionary["data"] for i in range(0,19): name=data_arr[i]["nickname"] img_url=data_arr[i]["vertical_src"] print(type(img_url)) respon_tem=urllib.request.urlopen(img_url) anchor_img=respon_tem.read() with open('../photos/'+name+'.jpg','wb') as f: f.write(anchor_img)
然后修改一下,讓它有了翻頁的功能
import urllib.parse import urllib import json import urllib.request data_info={} data_info['type']='AUTO' data_info['doctype']='json' data_info['xmlVersion']='1.6' data_info['ue']='UTF-8' data_info['typoResult']='true' data_info=urllib.parse.urlencode(data_info).encode('utf-8') for x in range(0,195): url='http://capi.douyucdn.cn/api/v1/getVerticalRoom?aid=ios&client_sys=ios&limit=20&offset='+str(x) print(data_info) requ=urllib.request.Request(url,data_info) requ.add_header('Referer','http://capi.douyucdn.cn') requ.add_header('User-Agent','DYZB/2.271 (iphone; iOS 9.3.2; Scale/3.00)') response=urllib.request.urlopen(requ) print(response) html=response.read().decode('utf-8') dictionary=json.loads(html) data_arr=dictionary["data"] for i in range(0,19): name=data_arr[i]["nickname"] img_url=data_arr[i]["vertical_src"] print(type(img_url)) respon_tem=urllib.request.urlopen(img_url) anchor_img=respon_tem.read() with open('../photos/'+name+'.jpg','wb') as f: f.write(anchor_img)
然后就等著吧~~
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- python協(xié)程gevent案例 爬取斗魚圖片過程解析
- python selenium爬取斗魚所有直播房間信息過程詳解
- Python使用Selenium模塊模擬瀏覽器抓取斗魚直播間信息示例
- python實現(xiàn)b站直播自動發(fā)送彈幕功能
- Python 使用 raise 語句拋出異常的流程分析
- Python爬蟲自動化爬取b站實時彈幕實例方法
- 使用Python獲取愛奇藝電視劇彈幕數(shù)據(jù)的示例代碼
- 詳解python爬取彈幕與數(shù)據(jù)分析
- Python腳本如何在bilibili中查找彈幕發(fā)送者
- Python爬蟲爬取Bilibili彈幕過程解析
- Python爬蟲 bilibili視頻彈幕提取過程詳解
- python3寫爬取B站視頻彈幕功能
- python基于selenium爬取斗魚彈幕
相關(guān)文章
pandas series序列轉(zhuǎn)化為星期幾的實例
下面小編就為大家分享一篇pandas series序列轉(zhuǎn)化為星期幾的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04基于python實現(xiàn)地址和經(jīng)緯度轉(zhuǎn)換
這篇文章主要介紹了基于python實現(xiàn)地址和經(jīng)緯度轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05在Python中使用swapCase()方法轉(zhuǎn)換大小寫的教程
這篇文章主要介紹了在Python中使用swapCase()方法轉(zhuǎn)換大小寫的教程,是Python入門中的基礎(chǔ)知識,需要的朋友可以參考下2015-05-05