当前位置:编程学习 > C#/ASP.NET >>

HtmlAgilityPack.dll爬虫获取百度音乐批量下载地址(C#源码)

定义一首歌曲的结构
[csharp]  
public class sMp3  
        {  
           public sMp3() { }  
           public string strSid;//sid  
           public string strDownPage;//下载页面  
           public string strEdition;//专辑名称  
           public string strSinger;//歌手名称  
           public string strUrl;//下载地址  
        }  
 
在一个列表中存放所有需要解析的歌曲地址
List<sMp3> listSongs = new List<sMp3>();
 
[csharp]  
新建一个HtmlAgilityPack.HtmlDocument对象 用来获取和解析HTML页面  
HtmlAgilityPack.HtmlDocument hdoc_Main = new HtmlAgilityPack.HtmlDocument();  
 
定义需要操作的XPath路径
[csharp] 
//主页  
        string Baidu_Music_MainPage_strUrl   = "http://music.baidu.com/";  
        string Baidu_Music_MainPage_Label    = "a";  
        string Baidu_Music_MainPage_Value    = "sid";  
        string Baidu_Music_MainPage_strXpath = "./html[1]/body[1]/div[4]/div[1]/div[1]/div[2]/div[3]/div[1]/div[1]";  
        //下载的页面 如 http://music.baidu.com/song/31496563/download  
        string Baidu_Music_DownMusic_strXpath="./html[1]/body[1]/div[1]/div[4]";           
        string Baidu_Music_DownMusic_Label   = "a";  
        string Baidu_Music_DownMusic_Value   = "href";  
  
        string Baidu_Music_DownMusic_strXpath_Title = "./html[1]/body[1]/div[1]/div[2]";           
        string Baidu_Music_DownMusic_Title   = "title";  
 
至于这些Xpath路径是怎么来的..可以通过查看源代码的方式,.也可以通过我之前发布的XPath工具(XPathTool)来查看得到。
XpathTool介绍地址  http://blog.csdn.net/witch_soya/article/details/8486893
XPathTool下载地址  http://download.csdn.net/detail/witch_soya/4978587
 
核心函数之一 :getLabelVal  这个函数接受一个 HtmlAgilityPack.HtmlDocument,实际上就是指定了要解析的HTML文件,strXpath是指定的Xpath语句,这样就能通过Xpath截取HTML中一部分内容了。然后通过 strLabel和strValue 两个参数在截取出来的内容中提取需要的指定标签的指定属性值。比如  HtmlAgilityPack.HtmlDocument 加载了一个html文档,通过strXpath就获取了该HTML文档中的一个区域  如果指定strLabel为 “a”strValue为”href”那么,返回的Arraylist就是该区域中a标签的所有超链接地址
 
[csharp]  
private ArrayList getLabelVal(HtmlAgilityPack.HtmlDocument dc, string strXpath, string strLabel, string strValue)  
        {  
            ArrayList Arr_Label_Val = new ArrayList();  
            //获取指定的节点  
            HtmlNode node = dc.DocumentNode.SelectSingleNode(strXpath);  
            if (node == null)  
            {  
                return null;  
            }  
            string strXPathLabel_Val = "descendant::" + strLabel;  
 
           #region   
            try  
            {  
                //HtmlNodeCollection atts  = node.SelectNodes("//*[@background or @lowsrc or @src or @href]");  
                //这样得到的是基于全文的  
                //HtmlNodeCollection hrefs = node.SelectNodes("//a[@href]");  
                //这样得到的是基于本节点的                                  
                HtmlNodeCollection hrefs = node.SelectNodes(strXPathLabel_Val);  
                if (hrefs == null)  
                {  
                    return null;  
                }  
  
                foreach (HtmlNode href in hrefs)  
                {  
                    if (href.Attributes[strValue] == null)  
                    {  
                        continue;  
                    }  
                    //这里得到了歌曲的sid  
                    String strSid = href.Attributes[strValue].Value;  
                    Arr_Label_Val.Add(strSid);                      
                }  
  
            }  
            catch (System.Exception ex)  
            {  
                MessageBox.Show(ex.ToString());  
            }  
            finally  
            {  
                // f2.Show();  
                  
            }  
        #endregion  
            return Arr_Label_Val;  
        }  
 
 
 
 
[csharp]  
//获取百度音乐地址  
        private void button1_Click(object sender, EventArgs e)  
        {       
 
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,