[英]What is the fastest way to get an HTML document node using XPath and the HtmlAgilityPack?
[英]Can't get body node from html document using xpath
我知道这是一个愚蠢的问题,但是我找不到它的问题。 无法使用xpath
从html文档的body节点获取内部文本。
我正在使用的代码:
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(String.Format(url, companyName));
HtmlNode node = doc.DocumentNode.SelectSingleNode("//body");
string code = null;
try
{
code = node.FirstChild.InnerText;
}
catch (NullReferenceException e)
{
}
网站结构 :
<html>
<body>PYRIX | preston york</body>
</html>
我只是尝试了node.InnerText
而已。 问题必须出在如何加载文档上。
这有效:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("<html><body>PYRIX | preston york</body></html>");
var node = doc.DocumentNode.SelectSingleNode("//body");
Console.WriteLine(node.InnerText);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.