I'm following the answer given here: https://stackoverflow.com/a/18066671

string Url = "http://www.owgr.com/ranking";
        HtmlWeb web = new HtmlWeb();
        HtmlDocument doc = web.Load(Url);
        string worldRanking = doc.DocumentNode.SelectNodes("//*[@id=\"ranking_table\"]/div[2]/table/tbody/tr[1]/td[1]")[0].InnerText;

But the Xpath is returning a null reference. I've tried several variations but nothing's working, I've never used XPath before, am I missing something?

有帮助吗?

解决方案

There is no <tbody/> element in that table, remove the /tbody axis step and your query works totally fine. See "Why does my XPath query (scraping HTML tables) only work in Firebug, but not the application I'm developing?" for details.

Additional hint: XPath also supports single quotes for strings, so you don't need to escape the double quotes.

This XPath expression will return the element you're looking for:

//*[@id='ranking_table']/div[2]/table/tr[1]/td[1]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top