利用python爬取斗魚app中照片方法實(shí)例
前言
沒想到python是如此強(qiáng)大,令人著迷,以前看見圖片總是一張一張復(fù)制粘貼,現(xiàn)在好了,學(xué)會(huì)python就可以用程序?qū)⒁粡垙垐D片,保存下來(lái)。
最近看到斗魚里的照片都不錯(cuò),決定用最新學(xué)習(xí)的python技術(shù)進(jìn)行爬取,下面將實(shí)現(xiàn)的過(guò)程分享出來(lái)供大家參考,下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
方法如下:
首先下載一個(gè)斗魚(不下載也可以,url都在這了對(duì)吧)
通過(guò)抓包,抓取到一個(gè)json的數(shù)據(jù)包,得到下面的地址
觀察測(cè)試可知,通過(guò)修改offset值就是相當(dāng)于app的翻頁(yè)
訪問這個(gè)url,返回得到的是一個(gè)大字典,字典里面兩個(gè)索引,一個(gè)error,一個(gè)data。而data又是一個(gè)長(zhǎng)度為20的數(shù)組,每個(gè)數(shù)組又是一個(gè)字典。每個(gè)字典中又有一個(gè)索引,vertical_src。
我們的目標(biāo)就是它了!
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ù)了。然后再通過(guò)對(duì)這json代碼的切片,分離得到每個(gè)主播照片的url地址。
然后得到這一頁(yè)的照片
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)
然后修改一下,讓它有了翻頁(yè)的功能
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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- python協(xié)程gevent案例 爬取斗魚圖片過(guò)程解析
- python selenium爬取斗魚所有直播房間信息過(guò)程詳解
- Python使用Selenium模塊模擬瀏覽器抓取斗魚直播間信息示例
- python實(shí)現(xiàn)b站直播自動(dòng)發(fā)送彈幕功能
- Python 使用 raise 語(yǔ)句拋出異常的流程分析
- Python爬蟲自動(dòng)化爬取b站實(shí)時(shí)彈幕實(shí)例方法
- 使用Python獲取愛奇藝電視劇彈幕數(shù)據(jù)的示例代碼
- 詳解python爬取彈幕與數(shù)據(jù)分析
- Python腳本如何在bilibili中查找彈幕發(fā)送者
- Python爬蟲爬取Bilibili彈幕過(guò)程解析
- Python爬蟲 bilibili視頻彈幕提取過(guò)程詳解
- python3寫爬取B站視頻彈幕功能
- python基于selenium爬取斗魚彈幕
相關(guān)文章
Python程序自動(dòng)以管理員權(quán)限運(yùn)行的實(shí)現(xiàn)方法
Windows上為了安全起見,python語(yǔ)言啟動(dòng)的應(yīng)用程序默認(rèn)都不會(huì)使用admin管理員權(quán)限,但是在有些情況下我們又需要使用管理員權(quán)限啟動(dòng)應(yīng)用,這篇文章主要給大家介紹了關(guān)于Python程序自動(dòng)以管理員權(quán)限運(yùn)行的實(shí)現(xiàn)方法,需要的朋友可以參考下2023-11-11
Python變量類型知識(shí)點(diǎn)總結(jié)
在本文里我們給大家分析一篇關(guān)于Python變量類型的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們跟著學(xué)習(xí)下。2019-02-02
python畫圖把時(shí)間作為橫坐標(biāo)的方法
今天小編就為大家分享一篇python畫圖把時(shí)間作為橫坐標(biāo)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
web自動(dòng)化測(cè)試Selenium點(diǎn)擊元素的常用方法
在Web自動(dòng)化測(cè)試中,Selenium提供多種點(diǎn)擊方法,常用的click()方法通過(guò)選中元素并觸發(fā)點(diǎn)擊事件,若click()方法不穩(wěn)定,可以采用JavaScript執(zhí)行點(diǎn)擊或使用ActionChains類模擬鼠標(biāo)點(diǎn)擊,需要的朋友可以參考下2024-09-09
解決jupyter notebook import error但是命令提示符import正常的問題
這篇文章主要介紹了解決jupyter notebook import error但是命令提示符import正常的問題,具有很好的參考2020-04-04
Python實(shí)現(xiàn)讀取csv文件并進(jìn)行排序
這篇文章主要為大家詳細(xì)介紹了一個(gè)python簡(jiǎn)單案例并進(jìn)行代碼展示,本文的案例是利用pandas庫(kù)實(shí)現(xiàn)讀取csv文件并按照列的從小到大進(jìn)行排序,需要的可以參考一下2023-02-02

