如何重新配置代码以输出特定值而不是集合
目前,我的应用程序上的这段代码非常适合循环浏览页面并抓取表单输入值的标签,然后将该值打印到富文本框。
我现在正在尝试修改代码,以便在标签包含特定名称时为我提供 ID 值。
例如,如果输入字段之前的标签包含单词“用户名”,那么我希望应用程序输出输入字段的 ID,而不仅仅是输出具有标签的所有内容。
这是我当前的代码:
Dim web As HtmlAgilityPack.HtmlWeb = New HtmlWeb()
Dim doc As HtmlAgilityPack.HtmlDocument = web.Load("http://shaggybevo.com/board/register.php")
Dim nodes As HtmlNodeCollection
' Keeps track of the labels by the associated control id
Dim labelText As New System.Collections.Generic.Dictionary(Of String, String)
' First, get the labels
nodes = doc.DocumentNode.SelectNodes("//label")
If nodes IsNot Nothing Then
For Each node In nodes
If node.Attributes.Contains("for") Then
Dim sFor As String
' Extract the for value
sFor = node.Attributes("for").Value
' If it does not exist in our dictionary, add it
If Not labelText.ContainsKey(sFor) Then
labelText.Add(sFor, node.InnerText)
End If
End If
Next
End If
nodes = doc.DocumentNode.SelectNodes("//input")
Dim sbText As New System.Text.StringBuilder(500)
If nodes IsNot Nothing Then
For Each node In nodes
' See if this input is associated with a label
If labelText.ContainsKey(node.Id) Then
' If it is, add it to our collected information
sbText.Append("Label = ").Append(labelText(node.Id))
sbText.Append(", Id = ").Append(node.Id)
sbText.AppendLine()
End If
Next
End If
RichTextBox1.Text = sbText.ToString
I currently this code on my app that works great to loop through a page and grab the label of an input value of a form and then print the value to a rich text box.
I am now trying to revamp the code to give me the ID the value if the label contains a specific name.
For instance if the label before the input field contains the word Username then I want the application to output the ID of the input field rather than just everything that has a label.
Here is my current code:
Dim web As HtmlAgilityPack.HtmlWeb = New HtmlWeb()
Dim doc As HtmlAgilityPack.HtmlDocument = web.Load("http://shaggybevo.com/board/register.php")
Dim nodes As HtmlNodeCollection
' Keeps track of the labels by the associated control id
Dim labelText As New System.Collections.Generic.Dictionary(Of String, String)
' First, get the labels
nodes = doc.DocumentNode.SelectNodes("//label")
If nodes IsNot Nothing Then
For Each node In nodes
If node.Attributes.Contains("for") Then
Dim sFor As String
' Extract the for value
sFor = node.Attributes("for").Value
' If it does not exist in our dictionary, add it
If Not labelText.ContainsKey(sFor) Then
labelText.Add(sFor, node.InnerText)
End If
End If
Next
End If
nodes = doc.DocumentNode.SelectNodes("//input")
Dim sbText As New System.Text.StringBuilder(500)
If nodes IsNot Nothing Then
For Each node In nodes
' See if this input is associated with a label
If labelText.ContainsKey(node.Id) Then
' If it is, add it to our collected information
sbText.Append("Label = ").Append(labelText(node.Id))
sbText.Append(", Id = ").Append(node.Id)
sbText.AppendLine()
End If
Next
End If
RichTextBox1.Text = sbText.ToString
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您只想将最后一个循环中的 if 语句更改为:
I think you just want to change the if statement in the last loop to: