python+playwright通过监听浏览器网络响应获取mp4视频地址并下载代码
代码语言:python
所属分类:其他
代码描述:python+playwright通过监听浏览器网络响应获取mp4视频地址并下载代码,用 Playwright 打开视频页,监听网络响应,抓到真实的视频地址(可能是 .mp4,也可能是 .m3u8)。若是 .mp4:带上必要的 Cookie/Referer/User-Agent 直接下载。若是 .m3u8:用 ffmpeg 带请求头合成 mp4。 环境准备 pip install playwright requests playwright install chromium 安装 ffmpeg(macOS
代码标签: python playwright 通过 监听 浏览器 网络 响应 获取 mp4 视频 地址 下载
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
#!/usr/local/python3/bin/python3 # -*- coding: utf-8 -* import asyncio, argparse, subprocess, sys from urllib.parse import urlparse import requests from playwright.async_api import async_playwright UA = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) " "AppleWebKit/537.36 (KHTML, like Gecko) " "Chrome/123.0.0.0 Safari/537.36") def download_direct_mp4(url: str, headers: dict, out_path: str): with requests.get(url, headers=headers, stream=True) as r: r.raise_for_status() total = int(r.headers.get("content-length", 0)) size = 0 with open(out_path, "wb") as f: for chunk in r.iter_content(chunk_size=1 << 14): if chunk: f.write(chunk) size += len(chunk) if total: done = int(30 * size / total) sys.stdout.write("\r[{}{}] {}/{} MB".format( "=" * done, " " * (30 - done), round(size/1e6,2), round(total/1e6,2))) sys.stdout.flush() print("\nSaved:", out_path) def ffmpeg_hls_to_mp4(m3u8_url: str, headers: dict, out_path: str): # 只传关键头即可;\r\n 作为多行头分隔 header_lines = [] for k in ("User-Agent", "Cookie&q.........完整代码请登录后点击上方下载按钮下载查看
网友评论0