如何重新配置​​代码以输出特定值而不是集合

发布于 2024-12-27 11:04:50 字数 1783 浏览 0 评论 0原文

目前,我的应用程序上的这段代码非常适合循环浏览页面并抓取表单输入值的标签,然后将该值打印到富文本框。

我现在正在尝试修改代码,以便在标签包含特定名称时为我提供 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛徒 2025-01-03 11:04:50

我认为您只想将最后一个循环中的 if 语句更改为:

' See if this input is associated with a label             
If labelText.ContainsKey(node.Id) Then
    Dim sLabel As String

    sLabel = labelText(nodeId)

    ' If it is, see if the label name is one that we are looking for
    If sLabel.IndexOf("username", StringComparison.InvariantCultureIgnoreCase) >= 0 OrElse sLabel.IndexOf("user", StringComparison.InvariantCultureIgnoreCase) >= 0 OrElse sLabel.IndexOf("u", StringComparison.InvariantCultureIgnoreCase) >= 0 Then
        ' Report the node associated 
        Console.WriteLine("Username is associated with node id " & node.Id)
        ' And bail out of the loop
        Exit For
     End If
End If

I think you just want to change the if statement in the last loop to:

' See if this input is associated with a label             
If labelText.ContainsKey(node.Id) Then
    Dim sLabel As String

    sLabel = labelText(nodeId)

    ' If it is, see if the label name is one that we are looking for
    If sLabel.IndexOf("username", StringComparison.InvariantCultureIgnoreCase) >= 0 OrElse sLabel.IndexOf("user", StringComparison.InvariantCultureIgnoreCase) >= 0 OrElse sLabel.IndexOf("u", StringComparison.InvariantCultureIgnoreCase) >= 0 Then
        ' Report the node associated 
        Console.WriteLine("Username is associated with node id " & node.Id)
        ' And bail out of the loop
        Exit For
     End If
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文