HTML敏捷包

HtmlAgilityPack

我正在尝试使用 HtmlAgilityPack 来获取任何具有 green 的元素。

感谢您的帮助。

样本HTML:

<span id="CPH_Main_ucArbitrage_headerTaInfData" class=" green ">0.46%</span>   

我用过的代码:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

HtmlAgilityPack.HtmlDocument docHtml = new HtmlWeb().Load("https://www.globes.co.il/portal/arbitrage/");
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

HtmlNode node = doc.DocumentNode.SelectSingleNode("//span[@id='CPH_Main_ucArbitrage_headerTaInfData']");

string value = (node == null) ? "Error, id not found": node.InnerHtml;
MessageBox.Show(value);
HtmlAgilityPack.HtmlDocument docHtml = new HtmlWeb().Load("https://www.globes.co.il/portal/arbitrage/");

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

您有一个 docHtml 变量已经加载 https://www.globes.co.il HTML,然后,您再次定义了一个新的 doc 变量,它初始化了一个空 [=16] =].

在这部分代码中:

HtmlNode node = doc.DocumentNode.SelectSingleNode

您引用的 doc 变量是空的!这就是您遇到错误的原因。你必须使用 docHtml.

HtmlNode node = docHtml.DocumentNode.SelectSingleNode