0

因此,我试图在论坛上获取在线用户列表。这是 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);
            }

谢谢。

4

1 回答 1

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

您正在解析的网站 url 的字符串值在哪里。

有关代码的解释,请查看http://htmlagilitypack.codeplex.com/上的文档

于 2013-01-08T01:09:57.720 回答