HtmlAgilityPack - 獲取div內容

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>標籤進行格式化。

我試過的代碼:

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

謝謝。

熱門答案

嘗試

 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/上的文檔。




許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因