日期:2014-05-18  浏览次数:20931 次

C#WinForm程序实现联网歌曲搜索
我想实现歌曲搜索,搜索的结果来自是酷狗,百度,等。用listBox等控件显示。怎样获取歌曲对应的歌手头像,就像酷狗一样。

------解决方案--------------------
我用的是开源类库HtmlAgilityPack,很强大的 这个类库可以用C#操作DOM
C# code

    private void button10_Click(object sender, EventArgs e)
        {
            if (ValidData.IsEmpty(textBox1.Text, "关键字不能为空,请核对!") == false) { textBox2.Focus(); return; }
            else if (ValidData.IsEmpty(textBox2.Text, "长度不能为空,请核对!") == false) { textBox2.Focus(); return; }
            else if (ValidData.IsNumber(textBox2.Text, "数量必须为整数") == false) { textBox2.Focus(); return; }

            if (int.Parse(textBox2.Text) > 100)
            {
                MessageBox.Show("数量不能超过100(百度内置上限)", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string url = "http://www.baidu.com/s?wd=" + textBox1.Text + "&rn=" + int.Parse(textBox2.Text) + "";
            HtmlAgilityPack.HtmlDocument hd = GetHtmlDocumentFromLink(url);

            this.Text = getTitle(hd);
            MainContent(hd);
       }


        public void MainContent(HtmlAgilityPack.HtmlDocument hd)
        {
            if (hd == null)
                return;
            richTextBox3.Text = "";
            //贴吧、新闻、商品搜索、软件
            SpecialType(hd);

                for (int i = 0; i < int.Parse(textBox2.Text)+10; i++)
                {
                    try
                    {
                        HtmlNode navNode = hd.GetElementbyId(i.ToString());
                        string allXPath = navNode.XPath + "/tr[1]/td[1]/h3[1]";
                        string title = hd.DocumentNode.SelectSingleNode(allXPath).InnerText;
                        richTextBox3.AppendText("标题:" + title.Trim() + "\r\n");
                        ////地址     
                        string adr = hd.DocumentNode.SelectSingleNode(allXPath + "/a[1]").Attributes["href"].Value;
                        richTextBox3.AppendText("地址:" + adr + "\r\n");

                        //百度快照日期
                        string pDateXPath = navNode.XPath + "/tr[1]/td[1]/font[1]/span[1]";
                        string pDate = hd.DocumentNode.SelectSingleNode(pDateXPath).InnerText;
                        if (pDate != string.Empty)
                        {
                            richTextBox3.AppendText("百度快照:" + pDate + "\r\n");
                            string dataP = Regex.Match(pDate, @"\d{4}([-/])\d{1,2}\1\d{1,2}").Value;
                            richTextBox3.AppendText("快照日期:" + dataP + "\r\n\r\n");
                        }
                        else
                            richTextBox3.AppendText("\r\n\r\n");
                    }
                    catch (Exception)
                    {
                        richTextBox3.AppendText("\r\n");
                    }
                }
            }