2
private void Extract(string url)
{
    HtmlWeb hw = new HtmlWeb();
    HtmlDocument doc = hw.Load(url);
    foreach (HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href]"))
    {

    }
}

我想从 html 文件中提取/解析所有链接。但我收到一个错误:

错误 8“HtmlAgilityPack.HtmlDocument”不包含“DocumentElement”的定义,并且找不到接受“HtmlAgilityPack.HtmlDocument”类型的第一个参数的扩展方法“DocumentElement”(您是否缺少 using 指令或程序集引用?)

编辑**

我这样做了:

private void Extract(string url)
        {
            StreamWriter w = new StreamWriter(@"d:\localpath\test.txt");
            HtmlWeb hw = new HtmlWeb();
            HtmlDocument doc = hw.Load(url);
            foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
            {  
                w.WriteLine(link);
            }
            w.Close();
        }

并使用它:

Extract(@"d:\localpath\Sat24_Cloudsheight_Europe.html");

但是我得到的是同一行的很多倍:

HtmlAgilityPack.HtmlNode HtmlAgilityPack.HtmlNode HtmlAgilityPack.HtmlNode HtmlAgilityPack.HtmlNode HtmlAgilityPack.HtmlNode HtmlAgilityPack.HtmlNode HtmlAgilityPack.HtmlNode HtmlAgilityPack.HtmlNode HtmlAgilityNodePack.HtmlNode HtmlAgilityPack.HtmlNode HtmlAgilityHtmlAgilityPack.HtmlNode HtmlAgilityPack.

我如何将解析的链接写入文本文件?

4

1 回答 1

5

更改DocumentElementDocumentNode

于 2013-10-29T23:20:05.250 回答