mitmproxy捕获Tunefind的m4a预览音频时连接中断求助
问题描述
我尝试从Tunefind下载m4a格式的预览音频文件,已在Windows上配置localhost:8080代理,安装mitmproxy及对应的.cer证书。但当代理捕获音频文件时,网络连接会被阻断,需关闭代理才能恢复。同时想知道有没有办法通过脚本自动下载这些预览文件——目前必须手动点击播放按钮才能在浏览器网络面板里看到对应URL。
捕获日志
[::1]:56371: POST http://149.154.167.255/api << 200 OK 88b [::1]:56371: POST http://149.154.167.255/api << 200 OK 88b [::1]:56308: GET https://api-v2.tunefind.com/forward?action=p0BNXDJVzXlSljXRu6G7CScvPiHm%252FLEFVIhMahaF%252FBui… HTTP/2.0 << HTTP/2.0 302 Found 0b [05:41:54.777][[::1]:56390] client connect [::1]:56371: POST http://149.154.167.255/api << 200 OK 88b [::1]:56371: POST http://149.154.167.255/api << 200 OK 88b [05:41:58.031][[::1]:56390] server connect audio-ssl.itunes.apple.com:443 (23.12.105.227:443) [::1]:56371: POST http://149.154.167.255/api << 200 OK 88b [05:41:58.200] Intercettato file audio: https://audio-ssl.itunes.apple.com/itunes-assets/AudioPreview125/v4/97/c9/b7/97c9b734-f2a7-c05d-88c9-6799dc358553/mzaf_1452710730924593330.plus.aac.p.m4a
运行代理的命令
mitmdump -s tunefind_proxy.py --listen-port 8080 --set console_eventlog_verbosity=debug
当前脚本内容
from mitmproxy import http import os import requests import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') save_dir = "C:\\Users\\Administrator\\Desktop\\scripts\\tunefind\\downloads" os.makedirs(save_dir, exist_ok=True) def request(flow: http.HTTPFlow) -> None: if flow.request.method == "GET": url = flow.request.pretty_url if "audio-ssl.itunes.apple.com" in url and url.endswith(".m4a"): logging.info(f"Intercettato file audio: {url}") download_audio(url) def download_audio(url): try: logging.debug(f"Tentativo di download del file: {url}") response = requests.get(url, stream=True, verify=False) response.raise_for_status() file_name = os.path.basename(url) logging.debug(f"Nome del file estratto: {file_name}") save_path = os.path.join(save_dir, file_name) logging.debug(f"Percorso completo del file: {save_path}") with open(save_path, 'wb') as file: for chunk in response.iter_content(chunk_size=8192): file.write(chunk) logging.info(f"File salvato: {save_path}") except requests.exceptions.RequestException as e: logging.error(f"Errore durante il download del file: {e}")
解决方案
1. 修复代理阻断问题
当前脚本的核心问题是:在request阶段单独发起新的requests.get请求,却未处理原请求的响应,导致浏览器一直等待代理返回内容,最终超时阻断连接。
修改思路:在response阶段直接保存代理已获取到的音频内容,既不阻断原连接,还能避免重复请求浪费带宽。
修改后的脚本:
from mitmproxy import http import os import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') save_dir = "C:\\Users\\Administrator\\Desktop\\scripts\\tunefind\\downloads" os.makedirs(save_dir, exist_ok=True) def response(flow: http.HTTPFlow) -> None: if flow.request.method == "GET": url = flow.request.pretty_url if "audio-ssl.itunes.apple.com" in url and url.endswith(".m4a"): logging.info(f"捕获到音频文件: {url}") # 直接从代理响应中保存内容,无需重复请求 file_name = os.path.basename(url) save_path = os.path.join(save_dir, file_name) try: with open(save_path, 'wb') as file: file.write(flow.response.content) logging.info(f"文件已保存: {save_path}") except Exception as e: logging.error(f"保存文件出错: {e}")
2. 自动触发音频预览(无需手动点击播放)
Tunefind的音频预览URL需点击播放后才会加载,自动获取可通过以下两种方式实现:
方式一:自动化工具模拟点击
用Selenium或Playwright打开目标页面,自动遍历并点击所有播放按钮,触发音频请求后由mitmproxy自动捕获保存。
示例(Playwright实现):
from playwright.sync_api import sync_playwright import time def auto_trigger_previews(url): with sync_playwright() as p: # 配置代理指向mitmproxy browser = p.chromium.launch(proxy={"server": "http://localhost:8080"}, headless=False) page = browser.new_page() page.goto(url) # 等待页面加载完成 time.sleep(3) # 遍历所有播放按钮并点击 play_buttons = page.query_selector_all('button[aria-label="Play"]') for btn in play_buttons: btn.click() # 等待音频请求完成 time.sleep(1) browser.close() # 替换为目标Tunefind页面URL auto_trigger_previews("https://www.tunefind.com/show/your-target-show")
方式二:直接调用Tunefind API获取URL
分析Tunefind的API请求逻辑,提取必要参数后直接构造请求获取所有音频预览链接,无需模拟点击。需注意API可能存在签名或认证机制,需从浏览器请求中提取action等关键参数。
注意事项
- 确保mitmproxy证书已正确添加到系统和浏览器的信任列表,避免HTTPS请求失败。
- 频繁请求可能触发反爬机制,建议添加适当延迟。
- 下载的预览音频仅可用于个人非商业用途,需遵守版权规定。
内容的提问来源于stack exchange,提问作者nathan never
相关产品推荐
相关产品推荐

