HtmlAgilityPack DocumentNode.SelectNodes returns null, shouldn't

雨燕双飞 提交于 2019-12-03 18:12:27

问题


I'm trying to scrape content from an example page using the HTML agility pack. The DocumentNode.SelectNodes is returning null for an XPath query when I think it shouldn't. Could someone tell me why? The code is:

HtmlDocument doc = new HtmlDocument();
string xpath = "//h1[@class='product-title fn']"; // note, it still returns 
                                                  // null even with "//div"
doc.OptionFixNestedTags = true;
HtmlNode.ElementsFlags.Remove("form");
HtmlNode.ElementsFlags.Remove("option");

HtmlNodeCollection coll = doc.DocumentNode.SelectNodes(xpath);

if (coll != null)
{
    // do stuff
}
else
{
    // not expecting it to be null unless no matches
}

回答1:


According to the upstream bug comments it is for consistency:

DarthObiwan wrote Jan 11 2011 at 9:27 PM

This has been covered before, this function is written to mimic the way the System.XML works. Doing so will cause a major breaking change and thus will probably be slated for 2.0

  • http://htmlagilitypack.codeplex.com/workitem/29175
  • http://htmlagilitypack.codeplex.com/workitem/25973


来源:https://stackoverflow.com/questions/8619724/htmlagilitypack-documentnode-selectnodes-returns-null-shouldnt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!