老实说,我不知道该怎么做,而且我很困难。我的代码可能不值得分享,但我会尽力提出问题。
<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
答案 0 :(得分:0)
考虑以下注释:
<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
并通过<获取位于第二个索引处的元素em> indexer 是这样的:
Dim username =
htmldoc.DocumentNode.SelectNodes(
"//ul/li/b[text()='Username:']")(1).NextSibling.InnerHtml