獲得
  • htmlAgilityPack VB.NET中的標籤

.net html-agility-pack vb.net

老實說,我不知道怎麼做,我很困難。我的代碼可能不值得分享,但我會盡力提出問題。

<div class="pricing-table">
    <div class="span4">
        <div class="plan">
            <h3>PPTP <span>$0</span></h3>
            <ul>
                <li><b>IP:</b> " (hidden)"</li>
                <li><b>Username:</b> " Guest"</li>
                <li><b>Password:</b> " (hidden)"</li>
                <li><b>Unlimited</b> " Bandwidth"</li>
                <li><b>Torrents</b> " Allowed"</li>
                <li><b>No</b> " Logging"</li>
            </ul>
        </div>
    </div>
</div>

(這是第二個)

<div class="span4">
    <div class="plan most-popular">
        <h3>Open<span>$0</span></h3>
        <div class="plan-ribbon-wrapper">
            <div class="plan-ribbon">
                Popular
            </div>
        </div>
        <ul>
            <li><b>Username:</b> user</li>
            <li><b>Password:</b> (hidden)</li>
            <li><b>TCP</b> 80, 443</li>
            <li><b>UDP</b> 53, 40000</li>
            <li><b>Unlimited</b> Bandwidth</li>
            <li><b>Torrents</b> Allowed</li>
            <li><b>No</b> Logging</li>
        </ul><a class="btn btn-large btn-primary" href=
        "%20(hidden)">Download</a>
    </div>
</div>
<ul>
    <li><b>Username:</b> user</li>
    <li><b>Password:</b> (hidden)</li>
    <li><b>TCP</b> 80, 443</li>
    <li><b>UDP</b> 53, 40000</li>
    <li><b>Unlimited</b> Bandwidth</li>
    <li><b>Torrents</b> Allowed</li>
    <li><b>No</b> Logging</li>
</ul>

使用此HTML代碼,我想複製用戶名和密碼並將其放入表單上的標籤中。

這是我的代碼

Imports HtmlAgilityPack
Public Class Form1
Dim Prev_Pass, Prev_User, Currt_Pass, Currt_User, Info
Dim counter As Integer
Dim web As New HtmlWeb()
Dim htmldoc As HtmlAgilityPack.HtmlDocument = New HtmlAgilityPack.HtmlDocument
Dim htmlnodes As HtmlAgilityPack.HtmlNodeCollection = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    htmldoc = web.Load("website")
    htmlnodes = htmldoc.DocumentNode.SelectNodes("//div[@class=""pricing-table""]")
    For Each node As HtmlAgilityPack.HtmlNode In htmlnodes
        Dim releaseNode = node.SelectSingleNode("//div[@class=""plan most-popular""]")
            Info = releaseNode.SelectSingleNode("//ul/li/b[contains(text(), 'Username: ')]").InnerText
    Next
    Label3.Text = Info
End Sub
End Class

一般承認的答案

請考慮以下註釋:

<li>
    <b>Username:</b> <!-- Use XPath to obtain this element. -->
    " user" <!-- This is the next sibling. -->
</li>

記住這些註釋:

您可以使用XPath獲取<b>元素,然後通過NextSibling屬性訪問用戶名或密碼文本,如下所示:

Dim username = 
    htmldoc.DocumentNode.SelectSingleNode(
        "//ul/li/b[text()='Username:']").NextSibling.InnerHtml

Dim password =
    htmldoc.DocumentNode.SelectSingleNode(
        "//ul/li/b[text()='Password:']").NextSibling.InnerHtml

如果要訪問第二個元素而不是第一個元素,那麼您應該使用更具體的XPath或使用SelectNodes並通過索引器獲取位於第二個索引處的元素,如下所示:

Dim username =
    htmldoc.DocumentNode.SelectNodes(
        "//ul/li/b[text()='Username:']")(1).NextSibling.InnerHtml


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