HtmlAgilityPack - Getting div content

c# html-agility-pack

Question

So I am trying to get a list of users online on a forum. Here is what the html looks like:

<!-- logged-in users -->
            <div id="wgo_onlineusers" class="wgo_subblock section">
                <h3 class="blocksubhead"><img src="images/metro/red/misc/users_online.png" alt="Currently Active Users" />Currently Active Users</h3>
                <div>
                    <p>There are currently <a href="online.php">3 users online</a>. <span class="shade">3 members and 0 guests</span></p>
                    <p>Most users ever online was 23, 01-06-2013 at <span class="time">12:09 PM</span>.</p>

                    <ol class="commalist" id="wgo_onlineusers_list">

                            <li><a class="username" href="http://website.com/member.php?u=13"><span class="vip_username">Duncanrp</span></a>, </li>

                            <li><a class="username" href="http://website.com/member.php?u=17"><span class="regular_username">Jessica</span></a></li>

                    </ol>

                </div>
            </div>
            <!-- end logged-in users -->

Is it possible to get each individual user that is online using the HtmlAgilityPack? The users are formatted using the <li> tags.

Code I have attempted:

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

            htmlDocument.LoadHtml("http://www.vizor.us/forum.php");

            List<string> onlineUsers = new List<string>();

            foreach (HtmlAgilityPack.HtmlNode selectNode in htmlDocument.DocumentNode.SelectNodes("//li/a[@class='username']"))
            {
                onlineUsers.Add(selectNode.InnerText);
            }

Thanks.

Popular Answer

Try

 HtmlDocument htmlDocument = new HtmlDocument();

 htmlDocument.LoadHtml("http://vizor.us/forum.php");

 List<string> onlineUsers = new List<string>();

foreach (HtmlNode selectNode in htmlDocument.DocumentNode.SelectNodes("//li/a[@class='username']")) {
    onlineUsers.Add(selectNode.InnerText);
            }
        }

where is the string value of the website url that you are parsing.

For explanation of code please review documentation at http://htmlagilitypack.codeplex.com/




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why