前言 ??
嗨嘍,大家好呀~這里是愛看美女的茜茜吶
小姐姐你們喜歡嗎?反正我是喜歡的,所以我決定!!
今天采集小姐姐視頻~保存下來供我欣賞
環境使用:
-
Python 3.8
-
Pycharm
模塊使用:
- import requests >>> pip install requests
內置模塊 你安裝好python環境就可以了
-
import re
-
import json
如果安裝python第三方模塊:
-
win + R 輸入 cmd 點擊確定, 輸入安裝命令 pip install 模塊名 (pip install requests) 回車
-
在pycharm中點擊Terminal(終端) 輸入安裝命令
基本思路流程: <實作采集案例基本 通用>
一. 資料來源分析
-
明確我們的需求 https://www.acfun.cn/v/ac35510357 這個視頻內容
-
分析我們視頻內容來自于哪里
通過開發者工具進行抓包分析:
-
F12 或者 滑鼠右鍵點擊檢查
-
分析資料在那個資料包里面 這個網站資料是不是m3u8需要自己判斷
m3u8 好處是什么, 你看多少內容就給你加載多少內容, 你看三秒就給你加載三秒, 綠色雙人愛情.....<>
正常的視頻內容: MP4 2分鐘18秒
m3u8 分片段 ---> 分為很多小片段 <ts檔案>, 一個小片段只有幾秒鐘的時間
我想要獲取整個視頻內容, 獲取所有ts檔案, 所有ts檔案又保存在m3u8的檔案里面
想要視頻內容 ---> 分片段 ts檔案 ---> m3u8檔案里面 ---> 在網頁源代碼里面
二. 代碼實作步驟: 爬蟲基本四大步驟:
-
發送請求, 對于視頻詳情頁url地址發送請求
-
獲取資料, 獲取服務器回傳回應資料
-
決議資料, 提取我們要的 m3u8檔案鏈接
-
發送請求, 對于 m3u8檔案鏈接 發送請求
-
獲取資料, 獲取服務器回傳回應資料
-
決議資料, 提取我們要的 所有ts檔案鏈接
-
保存資料, 把視頻片段全部保存下載, 合成為一個整體視頻內容
代碼
匯入模塊
# 匯入資料請求模塊 ---> 第三方模塊 需要 在cmd里面進行 pip install requests import requests # 匯入re正則模塊 ---> 內置模塊 不需要安裝 import re # 匯入json模塊 ---> 內置模塊 不需要安裝 import json # 匯入格式化輸出模塊 ---> 內置模塊 不需要安裝 from pprint import pprint # 匯入進度條模塊 ---> 第三方模塊 需要 在cmd里面進行 pip install tqdm from tqdm import tqdm # 匯入tk GUI模塊 import tkinter as tk import tkinter.messagebox
更多資料獲取加Q裙:261823976 點擊藍字加入【python學習裙】
def get_response(html_url): """ 發送請求函式 :param html_url: 請求鏈接 :return: 回應物件 """ # 偽裝瀏覽器 headers ---> 開發者工具里面復制粘貼 headers = { # 瀏覽器基本身份資訊 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36' } # 發送請求 <Response [200]> 回應物件 response = requests.get(url=html_url, headers=headers) return response def get_video_info(video_id): """ 獲取資訊資料 :param video_id: 視頻ID :return: 視頻資料 """ video_url = f'https://www.acfun.cn/v/ac{video_id}' response = get_response(video_url) # 提取視頻標題 title = re.findall('"title":"(.*?)",', response.text)[1] # 獲取m3u8 html_data = re.findall('window.pageInfo = window.videoInfo = (.*?);', response.text)[0] # 轉換資料型別 json_data = json.loads(html_data) # 字典取值, 鍵值對 ---> 根據冒號左邊的內容[鍵], 提取冒號右邊的內容[值] m3u8_url = \ json.loads(json_data['currentVideoInfo']['ksPlayJson'])['adaptationSet'][0]['representation'][0]['backupUrl'][0] # 4. 發送請求, 對于 m3u8檔案鏈接 發送請求 5. 獲取資料, 獲取服務器回傳回應資料 m3u8_data = get_response(m3u8_url).text # 6. 決議資料 m3u8_data = re.sub('#E.*', '', m3u8_data).split() # 串列推導式 ts_url_list = ['https://ali-safety-video.acfun.cn/mediacloud/acfun/acfun_video/' + ts for ts in m3u8_data] return title, ts_url_list def save(title, ts_url): """ 保存資料 :param title: 視頻標題 :param ts_url: ts鏈接 :return: """ ts_content = get_response(ts_url).content with open('video\\' + title + '.mp4', 'ab') as f: f.write(ts_content) def main(): """ 主函式 :param video_id: :return: """ video_id = Va.get() # 獲取視頻資料資訊 title, ts_url_list = get_video_info(video_id) for ts_url in tqdm(ts_url_list): save(title, ts_url) tk.messagebox.showinfo(title='溫馨提示', message=f'{title}下載完成') if __name__ == '__main__': # main('35556211') # 實體化物件 root = tk.Tk() # 設定標題 root.title('小視頻下載') # 設定大小 root.geometry('424x115+200+200') # 設定可變變數 Va = tk.StringVar() # 設定文字 tk.Label(root, text='僅提供學習交流', font=('黑體', 15)).grid(row=0, column=2) tk.Label(root, text='輸入視頻ac號: ', font=('黑體', 15)).grid(row=1, column=1) # 設定輸入框 tk.Entry(root, textvariable=Va).grid(row=1, column=2) # 設定按鈕 tk.Button(root, text='下載', font=('黑體'), command=main).grid(row=1, column=3) # 顯示視窗 root.mainloop()







尾語 ??
感謝你觀看我的文章吶~本次航班到這里就結束啦 ??
希望本篇文章有對你帶來幫助 ??,有學習到一點知識~
躲起來的星星??也在努力發光,你也要努力加油(讓我們一起努力叭),
最后,博主要一下你們的三連呀(點贊、評論、收藏),不要錢的還是可以搞一搞的嘛~
不知道評論啥的,即使扣個6666也是對博主的鼓舞吖 ?? 感謝 ??
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/501987.html
標籤:其他
上一篇:對于Java回圈中的For和For-each,哪個更快
下一篇:api進階Day2(低級流)檔案流的輸出流、讀取流。向檔案中寫入文本資料、讀取檔案中的字串、用lambda運算式創建檔案過濾器。
