如何使用自定义模块为每个人构建一个

时间:2018-03-12 10:48:32

标签: vb.net

您好我已经构建了一个自定义模块按类名获取元素但我无法使用每个函数。 好吧,我尝试通过classname调用我的元素的代码循环,这是一个自定义模块 通常它很容易获得循环,但使用这个定制模块,我找不到这个工作方式。 我是vb上的新手,我找不到任何可靠的例子 这是我需要执行循环的代码 我需要一些像这样的东西

  

对于ele中的每个cuele

     

下一步

但它给我一个错误

  

Expression的类型为'System.Windows.Forms.HtmlElement',它不是集合类型

我尝试了不同的方法,但我得到了一个空例外

Imports System.Text
Imports System.Net
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form3
Dim WithEvents htmldoc As HtmlDocument


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'TextBox1.Text = ""

    WebBrowser1.Navigate(TextBox1.Text)

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    htmldoc = WebBrowser1.Document

    'WebBrowser1.Document.Forms(0).InvokeMember("click")
    'WebBrowser1.Document.Forms(0).InvokeMember("submit")

    Dim allelements As HtmlElementCollection = WebBrowser1.Document.All

    For Each webpageelement As HtmlElement In allelements


        'ListBox1.Items.Add(webpageelement.GetAttribute("title").ToString)
        'ListBox1.Items.Add(webpageelement.GetAttribute("href").ToString)
        Dim PageElement As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("time")
        For Each CurElement As HtmlElement In PageElement
            'TextBox1.Text = TextBox1.Text + CurElement.GetAttribute("Liga NOS") + Environment.NewLine
            'ListBox2.Items.Add(CurElement.GetAttribute("datetime") + Environment.NewLine)

        Next
        Dim PageElement2 As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("td")
        For Each CurElement As HtmlElement In PageElement2
            'TextBox1.Text = TextBox1.Text + CurElement.GetAttribute("Liga NOS") + Environment.NewLine
            'ListBox1.Items.Add(CurElement.GetAttribute("data-content") + Environment.NewLine)

        Next
        Dim PageElement3 As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
        For Each CurElement As HtmlElement In PageElement3
            'TextBox1.Text = TextBox1.Text + CurElement.GetAttribute("Liga NOS") + Environment.NewLine
            'ListBox3.Items.Add(CurElement.GetAttribute("alt") + Environment.NewLine)

        Next




    Next
    For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1
        If ListBox1.GetItemText(ListBox1.Items(i)) = String.Empty Then
            ListBox1.Items.RemoveAt(i)
        End If
    Next i

    For i As Integer = ListBox2.Items.Count - 1 To 0 Step -1
        If ListBox2.GetItemText(ListBox2.Items(i)) = String.Empty Then
            ListBox2.Items.RemoveAt(i)
        End If
    Next i
    Dim ele As HtmlElement = WebBrowser1.Document.GetElementsByClassName("league-data")(0)
    'TextBox1.Text = ele.InnerText
    ListBox2.Items.Add(ele.InnerText)
    Dim ArrayTag As New ArrayList
For Each item As HtmlElement In 
Form1.WebBrowser1.Document.GetElementsByTagName("div")
ArrayTag.Add(item.InnerText)
Next
ListBox2.Items.Add(ArrayTag(64))


End Sub


End Class

1 个答案:

答案 0 :(得分:0)

好的这可能会奏效,但我无法测试它。我稍微更改了扩展名,因此它创建了一个列表而不是HtmlElementCollection(看起来这些是只读的)

<System.Runtime.CompilerServices.Extension()>
Public Function GetElementsByClassName(Source As HtmlElementCollection, ClassName As String) As List(Of HtmlElement)
    Dim output As New List(Of HtmlElement)
    For i As Integer = 0 To Source.Count - 1
        Try
            If Source(i).GetAttribute("className") = ClassName Then
                output.Add(Source(i))
            End If
        Catch ex As Exception
        End Try
    Next
    Return output
End Function

并修改了表单代码,以便它还处理列表而不是HtmlElementCollection

For Each cuele As HtmlElement In ele
    ListBox2.Items.Add(cuele.InnerText)
Next

希望这应该有效。

我还建议您使用列表而不是ArrayList,因为列表更快且强类型。

Dim ArrayTag As New List(Of String)
For Each item As HtmlElement In Form1.WebBrowser1.Document.GetElementsByTagName("div")
    ArrayTag.Add(item.InnerText)
Next
ListBox2.Items.Add(ArrayTag(64))