[转]使用.net里的WebClient自动下载baidu的mp3
WebClient webdown = new WebClient();
string url = "http://list.mp3.baidu.com/topso/mp3topsong.html?id=1?top2";
string tmp = webdown.DownloadString(url);
// RTBInfo.Text = tmp;
string scanStr = @"(?<urlstr>http://mp3.baidu.com/m[?]tn=baidump3&ct=(d+)(S+))(s*)(S+)>(?<songName>S+)<";//&(W<urlstr>+) target=_blank>(W<songName>+)</a>";
Regex regScan = new Regex(scanStr, RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection matchCol_F = regScan.Matches(tmp);
MatchCollection matchCol;
MatchCollection matchCol_S;
foreach (Match mat in matchCol_F)
...{
string songName = mat.Groups["songName"].ToString();
string songUrl = mat.Groups["urlstr"].ToString();
songUrl = songUrl.Remove(songUrl.Length - 1, 1);
if (songName == ")")
...{
continue;
}
RTBInfo.AppendText("下载歌曲:" + songName + " ");
try
...{
tmp = webdown.DownloadString(songUrl);
}
catch
...{
continue;
}
// http://220.181.38.82/m?ct=134217728&tn=baidusg,牛仔很忙 &word=mp3,http://jayhome.pengpeng.com/down/mp3/Y2Jja6OfrLKSm6Wpo5WBmap6oqGaZHqmMQ$$.mp3,,[%C5%A3%D7%D0%BA%DC%C3%A6+%D6%DC%BD%DC%C2%D7]&si=%C5%A3%D7%D0%BA%DC%C3%A6;;%D6%DC%BD%DC%C2%D7;;218633;;218633&lm=16777216
scanStr = @"(?<urlstr>http://(S+)(d+)(D+)" + songName + @"(s*)(D+)mp3(S+)&lm=(d+))";
regScan = new Regex(scanStr, RegexOptions.IgnoreCase | RegexOptions.Compiled);
matchCol = regScan.Matches(tmp);
if (matchCol.Count > 0)
songUrl = matchCol[0].Groups["urlstr"].ToString();
else
continue;
//RTBInfo.AppendText("下载地址2:" + songUrl + " ");
try
...{
tmp = webdown.DownloadString(songUrl);
}
catch
...{
continue;
}
scanStr = @"(?<urlstr>http://(S+)mp3)";
regScan = new Regex(scanStr, RegexOptions.IgnoreCase | RegexOptions.Compiled);
matchCol_S = regScan.Matches(tmp);
foreach (Match mat_S in matchCol_S)
...{
songUrl = mat_S.Groups["urlstr"].ToString();
// RTBInfo.AppendText("下载地址3:" + songUrl + " ");
RTBInfo.AppendText("下载中....");
try
...{
if (File.Exists("d:\new\" + songName + ".mp3")) break;
webdown.DownloadFile(songUrl, "d:\new\" + songName + ".mp3");
}
catch(Exception ex)
...{
RTBInfo.AppendText("下载失败 ");
continue;
}
RTBInfo.AppendText("下载成功 ");
break;
}
}
RTBInfo.AppendText("下载结束 ");