用到的控件有: msscript.ocx , msscrchs.dll 。 引用其中的 msscript.ocx 在项目中即可;
示例代码如下 :
代码#region 搜索歌曲的下载歌曲
/// <summary>
/// 从百度的试听页面获取歌曲的下载地址
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string DecodeMP3Url(string url)
{
string content = GetReponseText(url, Encoding.GetEncoding("gb2312"), string.Empty);
Regex objRegex = new Regex("var sertim =(?<sertimvalue>[\\S ]*?), statim =(?<statimvalue>[\\S ]*?);",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
Match objMatch = objRegex.Match(content);
if (objMatch.Success)
{
//获取变量定义
string vardefine = objMatch.Value;
objRegex = new Regex("function decode\\(url\\){(?<body>[\\S\\s]*?)return decurl;(?<g>[\\s]*?)}",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
objMatch = objRegex.Match(content);
if (objMatch.Success)
{
//获取加密的函数体
string functiondefine = objMatch.Value;
//获取加密后的下载地址列表
objRegex = new Regex("\"(?<t>[\\S]{4})://(?<g>[\\S]*?)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled);
objMatch = objRegex.Match(content);
List<string> pathlist = new List<string>();
while (objMatch.Success)
{
string v = objMatch.Value.Substring(1, objMatch.Value.Length - 2);
if (!v.Contains("...")
&& !pathlist.Contains(v)
&& !v.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase))
{
pathlist.Add(v);
}
objMatch = objMatch.NextMatch();
}
ScriptControl sc = new ScriptControl();
sc.Language = "JavaScript";
sc.AllowUI = false;
sc.AddCode(vardefine);
sc.AddCode(functiondefine);
foreach (string path in pathlist)
{
//解密
string realurl = sc.Eval(string.Format("decode('{0}')", path)).ToString();
if (CheckMusicUrl(realurl))
{
return realurl;
}
}
}
}
return string.Empty;
}
static string GetReponseText(string sUrl, Encoding encode, string sAgent)
{
HttpWebRequest request = HttpWebRequest.Create(sUrl) as HttpWebRequest;
//request.AllowAutoRedirect = false;
Uri uri = new Uri(sUrl);
request.Referer = "http://" + uri.Host;
request.UserAgent = sAgent;
string result;
WebResponse response = request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, encode))
{
result = reader.ReadToEnd();
}
}
response.Close();
return result;
}
上面的代码是采集百度的MP3音乐,因为百度的音乐URL是用JS脚本构造出来的
所以我就直接用他的脚本,在C#后台进行URL构造,然后抓取地址内容。

浙公网安备 33010602011771号