gpt4 book ai didi

c# - 为什么我收到 DocumentElement 不存在的错误?

转载 作者:行者123 更新时间:2023-12-02 04:46:36 24 4
gpt4 key购买 nike

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

}
}

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

Error 8 'HtmlAgilityPack.HtmlDocument' does not contain a definition for 'DocumentElement' and no extension method 'DocumentElement' accepting a first argument of type 'HtmlAgilityPack.HtmlDocument' could be found (are you missing a using directive or an assembly reference?)

编辑**

我这样做了:

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.HtmlNodeHtmlAgilityPack.HtmlNodeHtmlAgilityPack.HtmlNodeHtmlAgilityPack.HtmlNodeHtmlAgilityPack.HtmlNodeHtmlAgilityPack.HtmlNodeHtmlAgilityPack.HtmlNodeHtmlAgilityPack.HtmlNodeHtmlAgilityPack.HtmlNodeHtmlAgilityPack.HtmlNodeHtmlAgilityPack.HtmlNodeHtmlAgilityPack.HtmlNode

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

最佳答案

DocumentElement 更改为 DocumentNode

关于c# - 为什么我收到 DocumentElement 不存在的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19670903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com