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

Python爬蟲實戰(zhàn)之虎牙視頻爬取附源碼

 更新時間:2021年10月15日 09:09:46   作者:松鼠愛吃餅干  
讀萬卷書不如行萬里路,學(xué)的扎不扎實要通過實戰(zhàn)才能看出來,本篇文章手把手帶你爬取虎牙短視頻數(shù)據(jù),大家可以在實戰(zhàn)過程中查缺補漏,加深學(xué)習(xí)

知識點

  • 爬蟲基本流程
  • re正則表達式簡單使用
  • requests
  • json數(shù)據(jù)解析方法
  • 視頻數(shù)據(jù)保存

開發(fā)環(huán)境

  • Python 3.8
  • Pycharm

爬蟲基本思路流程: (重點) [無論任何網(wǎng)站 任何數(shù)據(jù)內(nèi)容 都是按照這個流程去分析]

1.確定需求 (爬取的內(nèi)容是什么東西?)

  • 都通過開發(fā)者工具進行抓包分析
  • 分析視頻播放url地址 是可以從哪里獲取到
  • 如果我們想要的數(shù)據(jù)內(nèi)容 是 音頻數(shù)據(jù)/視頻數(shù)據(jù) (media)
  • 雖然說知道視頻播放地址, 但是我們還需要知道這個播放地址 可以從什么地方獲取

2.發(fā)送請求, 用python代碼模擬瀏覽器對于目標地址發(fā)送請求

3.獲取數(shù)據(jù), 獲取服務(wù)器給我們返回的數(shù)據(jù)內(nèi)容

4.解析數(shù)據(jù), 提取我們想要數(shù)據(jù)內(nèi)容, 視頻標題/視頻url地址

5.保存數(shù)據(jù)

【付費VIP完整版】只要看了就能學(xué)會的教程,80集Python基礎(chǔ)入門視頻教學(xué)

點這里即可免費在線觀看

分析目標url

先打開一個視頻,查看id

請?zhí)砑訄D片描述

打開開發(fā)者工具,查找

拿到目標url

開始代碼

最開始還是線導(dǎo)入所需模塊

import requests # 數(shù)據(jù)請求模塊 pip install requests (第三方模塊)
import pprint # 格式化輸出模塊 內(nèi)置模塊 不需要安裝
import re # 正則表達式
import json

數(shù)據(jù)請求

def get_response(html_url):
    # 用python代碼模擬瀏覽器
    # headers 把python代碼進行偽裝
    # user-agent 瀏覽器的基本標識
    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

獲取視頻標題以及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'] # 視頻標題
    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ù), 也是還需要對于播放地址發(fā)送請求的
    # response.content 獲取響應(yīng)的二進制數(shù)據(jù)
    video_content = get_response(html_url=video_url).content
    new_title = re.sub(r'[\/:*?"<>|]', '_', title)
    # 'video\\' + title + '.mp4' 文件夾路徑以及文件名字 mode 保存方式 wb二進制保存方式
    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}頁的數(shù)據(jù)內(nèi)容')
        # python基礎(chǔ)入門課程 第一節(jié)課 講解的知識點 字符串格式化方法
        url = f'https://v.huya.com/g/all?set_id=31&order=hot&page={page}'
        main(url)

運行代碼,得到數(shù)據(jù)

到此這篇關(guān)于Python爬蟲實戰(zhàn)之虎牙視頻爬取附源碼的文章就介紹到這了,更多相關(guān)Python 爬取虎牙視頻內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論