一起来打造个正版音乐下载器(二)(三)

    这是一起来打造个正版音乐下载器(一)的续集...

现在说的是分析HTML和采集网页指定的内容....

经过分析HTML可以知道巨鲸网每首音乐都有个ID,根据这个ID就能下载到每个音乐了。。光用说的比较抽象...看具体例子把

 

代码
<ul class="clear">
<li class="gq_a">1.</li>
<li class="gq_b"><input type="checkbox" id='Song1' value='M0314784001' SongID='3440992' SongName='这叫爱'></li>
<li class="gq_c"><a href='http://www.top100.cn/Product/product.aspx?ProductID=M0314784001' target="_blank">这叫爱</a></li>
<li class="gq_d">
<a href=http://www.top100.cn/Product/singer.aspx?singerID=55633 class="a2" target=_blank><font style='color:#ff0000;background-color:yellow'>BY2</font></a>&nbsp;
</li>
<li class="gq_d"><a href='http://www.top100.cn/Product/product.aspx?ProductID=S0314784000' target="_blank">单曲 - 这叫爱</a></li>
<li class="gq_e">0:03:29</li>
<li class="gq_f"><a href='#' onclick='javascript:play("http://www.top100.cn/Audition/Play.htm?Productid=M0314784001")'><img src="http://image.top100.cn/webImg/button_listen.jpg" /></a></li>
<li class="gq_g"><a href='http://www.top100.cn/Payment/cart.aspx?Productid=M0314784001' target="_blank"><img src="http://www.top100.cn/Images/button_Buy.jpg" /></a></li>
<li class="gq_h"><a href="http://wlyy.mcprc.gov.cn/Admin/ComSelect.aspx" target =_blank ></a></li>
</ul> <br clear="all">

从上面我们就能写出正则表达式,而我搜索BY2网页地址是:http://search.top100.cn/searchdefault.aspx?keyword=by2

不难看出http://search.top100.cn/searchdefault.aspx?keyword=《搜索关键字》

原理大概就是这样......我还是贴出代码来给大家看吧

代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using System.Threading;
using System.Diagnostics;
using System.IO;

namespace iMusic
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}



private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() == "")
{
MessageBox.Show(
"请输入要查询的歌名");
}
else
{
listBox1.Items.Clear();
listID.Clear();
Thread s
= new Thread(sss);
s.Start();
}
}

List
<string> listID = new List<string>();
public void sss()
{
WebClient client
= new WebClient();
byte[] page = client.DownloadData("http://search.top100.cn/searchdefault.aspx?keyword=" + textBox1.Text);
string content = System.Text.Encoding.UTF8.GetString(page);

string regex = "<li class=\"gq_b\"><input type=\"checkbox\" id='.*' value='.*' SongID='.*' SongName='.*'>";
Regex re
= new Regex(regex);
MatchCollection matches
= re.Matches(content);

System.Collections.IEnumerator enu
= matches.GetEnumerator();
while (enu.MoveNext() && enu.Current != null)
{
Match match
= (Match)(enu.Current);


listID.Add(match.Value.Substring(match.Value.IndexOf(
"M"), 11));
listBox1.Items.Add(match.Value.Substring(match.Value.LastIndexOf(
"=") + 2, match.Value.LastIndexOf(">") - match.Value.LastIndexOf("=") - 3));



}
MessageBox.Show(
"查找完成,如果没有找到请换个关键词尝试");

}
string fileName="";
private void button2_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItems.Count < 1)
{
MessageBox.Show(
"没有选中的歌曲");
}
else
{


//web地址全名
string remoteUri = "http://www.top100.cn/Payment/ds.aspx?Productid=" + listID[listBox1.SelectedIndex].ToString();
//输入下载的文件名称
fileName = listBox1.Items[listBox1.SelectedIndex].ToString() + ".mp3";
//创建WebClient实例
WebClient myWebClient = new WebClient();
Uri uri
= new Uri(remoteUri);
//绑定下载事件,以便于显示当前进度
myWebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnDownloadProgressChanged);
//绑定下载完成事件,以便于计算总进度
myWebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(OnDownloadFileCompleted);

// 下载保存文件到程序运行目录下
myWebClient.DownloadFileAsync(uri, Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Temp", fileName));



}
}




private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{


progressBar1.Value
= e.ProgressPercentage;
label1.Text
= "已下载" + e.BytesReceived + "字节/总计" + e.TotalBytesToReceive + "字节";//一个label框,用来显示当前下载的数据
}

private void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{

if (MessageBox.Show("下载完成,是否打开文件", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// 打开文件
Process myProcess = new Process();

myProcess.StartInfo.FileName
= Application.StartupPath + "\\temp\\" + fileName;
myProcess.StartInfo.Verb
= "Open";
myProcess.StartInfo.CreateNoWindow
= true;
myProcess.Start();
}
progressBar1.Value
= 0;
}




}
}

完整的代码就是上面那个样子...有什么不懂的可以留言...

posted on 2010-04-14 15:26  豆浆咖啡  阅读(2425)  评论(38编辑  收藏  举报