使用Fiddler Classic抓包工具批量下载音频资料
1. 通过F12开发者工具,下载音频文件
浏览器打开音频列表->F12快捷键->网络->媒体,播放一个音频文件,右边媒体下生成一个音频文件,右击“在新标签页中打开”,可以下载这个音频文件。
2.通过Fiddler Classic抓包工具批量下载音频资料
每播放一个音频文件,页面就会向服务端发送一个get请求。浏览器打开get请求的URL地址,可下载这个音频文件。
播放了多个音频文件,页面发送多个get请求,我们现在通过Fiddler Classic抓包工具获取这多个get请求的URL地址,然后通过程序批量下载这些文件。
1) 下载Fiddler Classic工具,下载地址:Download Fiddler Web Debugging Tool for Free by Telerik
2)开启接收https请求
Fiddler Classic默认不接收https请求,需要开启一下。 Tools->Options->HTTPS
3)点击左下角,开启/关闭抓包。 显示“Capturing”为开启状态,显示空白为关闭状态。
4)点击“Remove all” 可清理抓取到的所有请求。
5)Fiddler Classic开启抓包-> 播放音频
Fiddler Classic抓取到音频播放的get请求,其中“audio/mp4”格式的请求数据,为想要获取到的url请求地址。
6)将播放音频抓到的所有地址,复制粘贴到excel表格中
7)通过程序过滤出“audio/mp4”格式的请求,并发送get请求,自动下载音频文件。
下图是创建的winform界面:
该界面的功能的实现:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.WebRequestMethods;
namespace Test
{
public partial class DownloadMP4 : Form
{
public DownloadMP4()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
tbxMP4.Text = "G:\\Downloads\\";
}
DataTable _dtExcel = null;
List<RespondModel> _list = new List<RespondModel>();
private void btnExcel_Click(object sender, EventArgs e)
{
_dtExcel = GetExcelData();
}
private void btnMP4_Click(object sender, EventArgs e)
{
if (_dtExcel == null) return;
string httpUrl = "https://audiopay.cos.tx.xmcdn.com";
if (_dtExcel.Rows?.Count > 0)
{
_list = new List<RespondModel>();
foreach (DataRow dr in _dtExcel.Rows)
{
if (dr["Content-Type"].ToString().Contains("mp4"))
{
if (_list.FindIndex(p => p.URL.Split('?').First().Split('/').Last() == dr["URL"].ToString().Split('?').First().Split('/').Last()) < 0)
{
RespondModel mod = new RespondModel();
mod.URL = httpUrl + dr["URL"].ToString();
//mod.ContentType = "audio/mp4";
_list.Add(mod);
}
}
}
}
int i = 0;
if (_list.Count > 0)
{
foreach (RespondModel resp in _list)
{
string fileName = tbxMP4.Text + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".mp4";
GetPostContent(resp.URL, fileName);
i += 1;
progressBar1.Value = i * 100 / _list.Count;
}
}
MessageBox.Show($"下载完成!文件数:{i}");
}
private DataTable GetExcelData()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "选择Excel文件";
ofd.InitialDirectory = System.Windows.Forms.Application.StartupPath + "\\";
ofd.Filter = "Excel文件 (*.xlsx)|*.xlsx|Excel文件 (*.xls)|*.xls|所有文件 (*.*)|*.*";
ofd.RestoreDirectory = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
string fileName = ofd.FileName;
tbxExcel.Text = fileName;
string connectionString = $"Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '{fileName}';Extended Properties=Excel 8.0";
if (fileName.Contains("xlsx"))
{
connectionString = $"Provider=Microsoft.Ace.OleDb.12.0; Data Source = '{fileName}';Extended Properties=Excel 12.0";
}
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
DataTable dtNames = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
string sql = $"select * from [{dtNames.Rows[0]["TABLE_NAME"]}]"; //[Sheet1$]
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
conn.Close();
return ds?.Tables[0];
}
return null;
}
private HttpClient _httpClient;
public void GetPostContent(string url, string localSavePath)
{
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "GET";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.Proxy = null;
// Get response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
Stream responseStream = myResponse.GetResponseStream();
Stream stream = new FileStream(localSavePath, FileMode.Create);
byte[] bArr = new byte[1024];
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
while (size > 0)
{
stream.Write(bArr, 0, size);
size = responseStream.Read(bArr, 0, (int)bArr.Length);
}
stream.Close();
responseStream.Close();
}
catch (System.Exception ex)
{
throw ex;
}
}
}
public class RespondModel
{
public string URL { get; set; }
public string ContentType { get; set; }
}
}