前言大家早好、午好、晚好吖 ? ~ 開(kāi)發(fā)環(huán)境:基本流程一. 數(shù)據(jù)來(lái)源分析 抓包 數(shù)據(jù)包 m3u8: 視頻流 m3u8文本類型文件 將 mp4 切分為n個(gè)片段(.ts片段視頻) 每一個(gè)片段為10秒鐘 二. 代碼實(shí)現(xiàn)(爬蟲(chóng)實(shí)現(xiàn)的基本流程) 發(fā)送請(qǐng)求 獲取數(shù)據(jù) 解析數(shù)據(jù) 保存數(shù)據(jù) 批量采集視頻
代碼展示這里網(wǎng)址我就屏蔽了,大家根據(jù)我上文添加完整 導(dǎo)入模塊 import requestsimport reimport timeimport execjsfrom tqdm import tqdm
請(qǐng)求頭(偽裝) vid = 'kDe0W2m4JO2MA4Bz'headers = { 'cookie': 'dy_did=6a87143ffbf77633191f343700031601; Hm_lvt_e99aee90ec1b2106afe7ec3b199020a7=1675325976; dy_did=6a87143ffbf77633191f343700031601', 'origin': 'https://v.****.com', 'referer': f'https://v.****.com/show/{vid}', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'}
""" 獲取point_id :param vid: 作品id
:return: point_id
""" point_url = f"https://v.****.com/wgapi/vod/front/video/secondary/info?hid={vid}" point_id = requests.get(point_url, headers=headers).json()['data']['vid'] return point_id
""" 獲取sign :param vid: 作品id
:param point_id: point_id
:param did: did
:param tt: 時(shí)間戳
:return: sign值
""" def get_sign(vid, point_id, did, tt): main_url = f'https://v.****.com/show/{vid}' html_data = requests.get(main_url, headers=headers).text title = re.findall('<title>(.*?)</title>', html_data)[0] f = "const CryptoJS = require('crypto-js');" + re.findall('<script> (var v.*?)</script>', html_data)[0] ctx = execjs.compile(f) result = ctx.call('ub98484234', point_id, did, tt) sign = result.split('sign=')[-1] return [sign, title]
""" 獲取m3u8鏈接 :param v: 固定參數(shù)
:param did: 固定參數(shù)
:param tt: 時(shí)間戳
:param sign: 加密參數(shù)
:param vid: 視頻id
:return: m3u8鏈接
""" def get_streamUrl(v, did, tt, sign, vid):
data = { 'v': v, 'did': did, 'tt': str(tt), 'sign': sign, 'vid': vid }
發(fā)送請(qǐng)求 url = 'https://v.****.com/api/stream/getStreamUrl' response = requests.post(url=url, headers=headers, data=data)
獲取數(shù)據(jù) m3u8_url = response.json()['data']['thumb_video']['high']['url'] return m3u8_url
""" 下載視頻 :param m3u8_url: 鏈接
:param title: 標(biāo)題
""" def download_m3u8(m3u8_url, title): sub_ts = m3u8_url.split('playlist')[0] m3u8_text = requests.get(m3u8_url).text ts_list = re.sub('#E.*', '', m3u8_text).split() for ts in tqdm(ts_list): ts_url = sub_ts+ts ts_data = requests.get(ts_url).content with open(f'{title}.mp4', mode='ab') as f: f.write(ts_data)
did = "10000000000000000000000000001501"tt = int(time.time()) v = '220320230203'point_id = get_point_id(vid) info = get_sign(vid, point_id, did, tt) sign = info[0] title = info[1] m3u8_url = get_streamUrl(v, did, tt, sign, vid) download_m3u8(m3u8_url, title)
尾語(yǔ) ??好了,今天的分享就差不多到這里了! 完整代碼、更多資源、疑惑解答直接點(diǎn)擊下方名片自取即可。 有更多建議或問(wèn)題可以評(píng)論區(qū)或私信我哦!一起加油努力叭(? ·_·)? 喜歡就關(guān)注一下博主,或點(diǎn)贊收藏評(píng)論一下我的文章叭?。?!
|