winforms 循环列表框中的每个项目并加载网页

gj3fmq9x  于 2023-02-13  发布在  其他
关注(0)|答案(1)|浏览(123)

我试图通过.net6+ windows窗体中的控件webview2加载一个新网页,我使用列表框提取任何单个项目并将其添加到URL以加载到webview上。

11
22
33
44
55

我想在按下一个按钮,一个循环开始加载一个接一个,这些项目的每一个像

WebView21.Source = New Uri("https://google.it" & ListBox1.Items.first & "rest of the url")

并且在网页被加载之后,它应该提取它的html来检查是否存在某个字符串

Dim html As String
       html = Await WebView21.ExecuteScriptAsync("document.documentElement.outerHTML;")
       If html.Contains("Not found") Then
           MsgBox("In Vacanza")
       Else
           MsgBox("Attivo")
       End If
   End Sub

然后返回到第二个列表框项目,加载WebView,检查HTML等等。
我的问题是如何循环WebView以便逐个选择每个项目,并在while中继续执行这些小操作?p.s.一旦循环到达最后一个列表框项目,是否可以从第一个项目重新开始?非常感谢
编辑1:
我在尝试

Private ReadOnly resetEvent As New ManualResetEvent(False)

    Async Sub scanWeb()
        For Each listBoxElem As String In ListBox1.Items
            resetEvent.Reset()
            AddHandler WebView2.CoreWebView2.NavigationCompleted, AddressOf OnNavigationCompleted
            WebView2.Source = New Uri("https://ikalogs.ru/tools/map/?page=1&server=22&world=10&state=active&search=city&allies%5B1%5D=&allies%5B2%5D=&allies%5B3%5D=&allies%5B4%5D=&nick=" & listBoxElem & "&ally=&island=&city=&x=&y=")
            Await Task.Run(Sub() resetEvent.WaitOne())
            RemoveHandler WebView2.CoreWebView2.NavigationCompleted, AddressOf OnNavigationCompleted
            Dim html As String
            html = Await WebView2.ExecuteScriptAsync("document.documentElement.outerHTML;")
            If html.Contains("Not found") Then
                DataGridView1.Rows.Add(listBoxElem, "IN vacanza")
            Else
                DataGridView1.Rows.Add(listBoxElem, "Attivo")
            End If
        Next
    End Sub

    Private Sub OnNavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs)
        resetEvent.Set()
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WebView2 = New WebView2()
        WebView2.EnsureCoreWebView2Async()
    End Sub

但似乎循环没有等待进程结束,而是直接进入下一个列表框项目...

k3bvogb1

k3bvogb11#

最好的方法是在for-each循环中枚举listBox项:(我添加了一个退出方式-简单的鼠标点击窗体-退出循环)

Dim wStop As Boolean
    
    sub scanWeb()
     Do
       For Each listBoxElem As String In ListBox1.Items
         WebView21.Source = New Uri("https://google.it" & listBoxElem & "rest of the url")
         'etc....
       Next
     Loop Until wStop = True
     wStop = False
    end sub
   
    'way to stop scan 
    Private Sub form_clic(sender As Object, e As MouseEventArgs) Handles MyBase.MouseClick
      wStop = True
    End Sub

更新**

webview 2控件是用来显示数据的,我对此没有经验。我还建议你使用一个更简单的方法,基于System .NET.WebClient()并与线程相关联。下面是一个有效的代码开始:

Dim wStop As Boolean
Dim i As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim BackProcess = New Thread(Sub() Me.scanWeb())
    BackProcess.Priority = ThreadPriority.Normal
    BackProcess.Start()
end sub

Sub scanWeb()
    Do While Not wStop
        Dim WC As New System.Net.WebClient()
        'change url for your need
        Dim url As String = "https://www.google.fr/search?q=" & ListBox1.Items(i)
        Dim s As System.IO.Stream = WC.OpenRead(url)
        Dim sr As New System.IO.StreamReader(s)
        Dim html As String = sr.ReadToEnd()
        If html.Contains("Not found") Then 'beginInvoke allows the back task to communicate with UI
        Me.BeginInvoke(Sub() DataGridView1.Rows.Add(ListBox1.Items(i), "In vacanza"))
        Else
        Me.BeginInvoke(Sub() DataGridView1.Rows.Add(ListBox1.Items(i), "Attivo"))
    End If
    
    i += 1
    If i > ListBox1.Items.Count - 1 Then i = 0
Loop
End Sub
    
  'button event to stop scanning
Private Sub stopScan_Click(sender As Object, e As EventArgs) Handles stopScan.Click
   wStop = True
End Sub

相关问题