請點擊此處輸入圖片描述 Python下載知乎視頻。 # -*- coding: utf-8 -*- """ 下載知乎視頻: 依賴: pip install requests mac 安裝 ffmpeg: brew install ffmpeg """ import re import uuid im ...
請點擊此處輸入圖片描述
Python下載知乎視頻。
# -*- coding: utf-8 -*-
"""
下載知乎視頻:
依賴:
pip install requests
mac 安裝 ffmpeg:
brew install ffmpeg
"""
import re
import uuid
import subprocess
import requests
# 下邊 cookie 請打開知乎打開瀏覽器開發者工具隨便找一個請求複製 cookie,千萬不要泄露出去
HEADERS = {
'cookie': '' # TODO
}
QUALITY = 'ld' # 支持是 'ld' 'sd' 'hd' 分別是低清、中清、高清
def get_video_ids_from_url(url):
"""
回答或者文章的 url
"""
html = requests.get(url, headers=HEADERS).text
video_ids = re.findall(r'data-lens-id="(\d+)"', html)
if video_ids:
return set([int(video_id) for video_id in video_ids])
return []
def yield_video_m3u8_url_from_video_ids(video_ids):
for video_id in video_ids:
api_video_url = 'https://lens.zhihu.com/api/videos/{}'.format(int(video_id))
r = requests.get(api_video_url, headers=HEADERS)
playlist = r.json()['playlist']
m3u8_url = playlist[QUALITY]['play_url']
yield m3u8_url
def download(url):
video_ids = get_video_ids_from_url(url)
m3u8_list = list(yield_video_m3u8_url_from_video_ids(video_ids))
filename = '{}.mp4'.format(uuid.uuid4())
for idx, m3u8_url in enumerate(m3u8_list):
print('download {}'.format(m3u8_url))
subprocess.call(['ffmpeg', '-i', m3u8_url, filename.format(str(idx))])
if __name__ == '__main__':
# 貼上你需要下載的 回答或者文章的鏈接
url = '--------'
download(url)