-1

这次我需要一些 C# 方面的帮助。

我有一个 html :

<ul class="ui_sug_list"></ul></div></div></div></form>
</div></div><div class="cnt_listas"><ol id="listagem1" 
class="cols_2"><li><a href="/laura-pausini/73280/">16/5/74
</a></li><li><a href="/laura-pausini/73280/traducao.html">
16/5/74 (tradução)</a></li><li><a href="/laura-pausini/1566533/">16/5/74
(Spanish Version)</a></li><li><a href="/laura-pausini/1566533/traducao.html">
16/5/74 (Spanish Version) (tradução)</a></li><li><a href="/laura-pausini/1991556/">
A Simple Vista</a></li><li><a href="/laura-pausini/1991556/traducao.html">
A Simple Vista (tradução)</a></li>

我下载了一个这样的html,它没有来自网络的表格。我只需要打印歌曲的名称和指向歌曲的链接。我不知道如何从文件中获取这些信息。

这是我下载文件的方式:

        // Realiza Download do arquivo
        WebClient webClient = new WebClient();
        webClient.DownloadFile(
        "http://letras.mus.br/" + termo_busca + "/", @"C:\Temp\letras.html");

你能帮我个忙吗?

4

1 回答 1

2

您绝对应该使用HTML Agility Pack

您可以像这样获取链接和链接值:

 var doc = new HtmlAgilityPack.HtmlDocument();
 doc.LoadHtml(Html);
 foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
 {
    var value = link.Attributes["href"].Value; //gives you the link
    var text = link.InnerText; //gives you the text of the link
 }

你也可以使用这个也使用 html 敏捷包的类:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;

namespace Foo.Client
{
    public class Website
    {
        public string Html { get; private set; }

        private Website(string html)
        {
            Html = html;
        }

        public static Website Load(Uri uri)
        {
            validate(uri);
            return new Website(getPageContentFor(uri));
        }

        public List<string> GetHyperLinks()
        {
            var doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(Html);
            return extractLinksFrom(doc.DocumentNode.SelectNodes("//a[@href]"));
        }

        private static string getPageContentFor(Uri uri)
        {
            try
            {
                var request = (HttpWebRequest)WebRequest.Create(uri);
                var response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    return reader.ReadToEnd();
            }
            catch (WebException)
            {
                return String.Empty;
            }
        }

        private List<string> extractLinksFrom(HtmlNodeCollection nodes)
        {
            var result = new List<string>();
            if (nodes == null) return result;
            foreach (var link in nodes)
                    result.Add(link.Attributes["href"].Value);
            return result;
        }

        private static void validate(Uri uri)
        {
            if (!uri.IsAbsoluteUri)
                throw new ArgumentException("invalid uri format");
        }
    }
}
于 2012-09-24T20:13:19.513 回答