Python爬蟲實(shí)戰(zhàn)之虎牙視頻爬取附源碼
知識(shí)點(diǎn)
- 爬蟲基本流程
- re正則表達(dá)式簡(jiǎn)單使用
- requests
- json數(shù)據(jù)解析方法
- 視頻數(shù)據(jù)保存
開發(fā)環(huán)境
- Python 3.8
- Pycharm
爬蟲基本思路流程: (重點(diǎn)) [無(wú)論任何網(wǎng)站 任何數(shù)據(jù)內(nèi)容 都是按照這個(gè)流程去分析]
1.確定需求 (爬取的內(nèi)容是什么東西?)
- 都通過開發(fā)者工具進(jìn)行抓包分析
- 分析視頻播放url地址 是可以從哪里獲取到
- 如果我們想要的數(shù)據(jù)內(nèi)容 是 音頻數(shù)據(jù)/視頻數(shù)據(jù) (media)
- 雖然說知道視頻播放地址, 但是我們還需要知道這個(gè)播放地址 可以從什么地方獲取
2.發(fā)送請(qǐng)求, 用python代碼模擬瀏覽器對(duì)于目標(biāo)地址發(fā)送請(qǐng)求
3.獲取數(shù)據(jù), 獲取服務(wù)器給我們返回的數(shù)據(jù)內(nèi)容
4.解析數(shù)據(jù), 提取我們想要數(shù)據(jù)內(nèi)容, 視頻標(biāo)題/視頻url地址
5.保存數(shù)據(jù)
【付費(fèi)VIP完整版】只要看了就能學(xué)會(huì)的教程,80集Python基礎(chǔ)入門視頻教學(xué)
點(diǎn)這里即可免費(fèi)在線觀看
分析目標(biāo)url
先打開一個(gè)視頻,查看id
打開開發(fā)者工具,查找
拿到目標(biāo)url
開始代碼
最開始還是線導(dǎo)入所需模塊
import requests # 數(shù)據(jù)請(qǐng)求模塊 pip install requests (第三方模塊) import pprint # 格式化輸出模塊 內(nèi)置模塊 不需要安裝 import re # 正則表達(dá)式 import json
數(shù)據(jù)請(qǐng)求
def get_response(html_url): # 用python代碼模擬瀏覽器 # headers 把python代碼進(jìn)行偽裝 # user-agent 瀏覽器的基本標(biāo)識(shí) headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36' } # 用代碼直接獲取的 一般大多數(shù)都是直接 cookie response = requests.get(url=html_url, headers=headers) return response
獲取視頻標(biāo)題以及url地址
def get_video_info(video_id): html_url = f'https://liveapi.huya.com/moment/getMomentContent?videoId={video_id}&uid=&_=1634127164373' response = get_response(html_url) title = response.json()['data']['moment']['title'] # 視頻標(biāo)題 video_url = response.json()['data']['moment']['videoInfo']['definitions'][0]['url'] video_info = [title, video_url] return video_info
獲取視頻id
def get_video_id(html_url): html_data = get_response(html_url).text result = re.findall('<script> window.HNF_GLOBAL_INIT = (.*?) </script>', html_data)[0] # 需要把獲取的字符串?dāng)?shù)據(jù), 轉(zhuǎn)成json字典數(shù)據(jù) json_data = json.loads(result)['videoData']['videoDataList']['value'] # json_data 列表 里面元素是字典 # print(json_data) video_ids = [i['vid'] for i in json_data] # 列表推導(dǎo)式 # lis = [] # for i in json_data: # lis.append(i['vid']) # print(video_ids) # print(type(json_data)) return video_ids # 目光所至 我皆可爬 def main(html): video_ids = get_video_id(html_url=html) for video_id in video_ids: video_info = get_video_info(video_id) save(video_info[0], video_info[1])
保存數(shù)據(jù)
def save(title, video_url): # 保存數(shù)據(jù), 也是還需要對(duì)于播放地址發(fā)送請(qǐng)求的 # response.content 獲取響應(yīng)的二進(jìn)制數(shù)據(jù) video_content = get_response(html_url=video_url).content new_title = re.sub(r'[\/:*?"<>|]', '_', title) # 'video\\' + title + '.mp4' 文件夾路徑以及文件名字 mode 保存方式 wb二進(jìn)制保存方式 with open('video\\' + new_title + '.mp4', mode='wb') as f: f.write(video_content) print('保存成功: ', title)
調(diào)用函數(shù)
if __name__ == '__main__': # get_video_info('589462235') video_info = get_video_info('589462235') save(video_info[0], video_info[1]) for page in range(1, 6): print(f'正在爬取第{page}頁(yè)的數(shù)據(jù)內(nèi)容') # python基礎(chǔ)入門課程 第一節(jié)課 講解的知識(shí)點(diǎn) 字符串格式化方法 url = f'https://v.huya.com/g/all?set_id=31&order=hot&page={page}' main(url)
運(yùn)行代碼,得到數(shù)據(jù)
到此這篇關(guān)于Python爬蟲實(shí)戰(zhàn)之虎牙視頻爬取附源碼的文章就介紹到這了,更多相關(guān)Python 爬取虎牙視頻內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。