HtmlAgilityPack - Get DIV content

c# html html-agility-pack scrape string

Question

In WinForms C#, I'm attempting to extract some text from a DIV using HTMLAgilityPack.

My code is

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml("http://www.tibia.com/news/?subtopic=latestnews");
var res = doc.DocumentNode.SelectSingleNode("//div[@id='PlayersOnline']");
var content = res.InnerHtml;

// Print content
MessageBox.Show(content);

I'm attempting to download material from: http://www.tibia.com/news/?subtopic=latestnews

The number of "Players Online" is shown in a box at the top right of the page. I wish to get that sum.

The website's HTML looks like this:

<div id="PlayersOnline" onclick="window.location = 'https://secure.tibia.com/community/?subtopic=worlds';">11723<br>Players Online</div>

therefore I'm hoping to11723 as a result. No matter whether I get the entire:11723<br>Players Online as a result. I could just divide the text afterwards or use a regex match to ignore the br tag.

However, none of my code is operating, and I'm not sure why. The program freezes and states

System.NullReferenceException: 'Object reference not set to an instance of an object.'

<res>5__8 was null.
1
2
5/19/2017 8:33:41 PM

Popular Answer

Modify these rows:

    HtmlAgilityPack.Web webSite = new HtmlAgilityPack.Web();
    HtmlAgilityPack.HtmlDocument document = webSite.Load("http://www.tibia.com/news/?subtopic=latestnews");

    string content = document.GetElementbyId("PlayersOnline").OuterHtml;
2
10/3/2019 2:48:10 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow