HtmlAgilityPack - 获取div内容

时间:2013-01-08 00:47:40

标签: c# html-agility-pack

所以我想在论坛上在线获取用户列表。这是html的样子:

<!-- 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 -->

是否可以使用HtmlAgilityPack获取在线的每个用户?用户使用<li>标记格式化。

我尝试过的代码:

            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);
            }

感谢。

1 个答案:

答案 0 :(得分:1)

尝试

 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);
            }
        }

您要解析的网站网址的字符串值。

有关代码的说明,请查看http://htmlagilitypack.codeplex.com/

上的文档