所以说我想找到“你好,世界!”这个词。在网页上。它的 ID 为“文本”。使用名为 WebBrowser1 的 WebBrowser,如果该文本存在于网页上,是否有方法返回 true,或者如果它不存在,它将返回 false?无论哪种方式,URL 都将完全相同。
编辑:到达它的 HTML 路径很长,所以我需要在页面上找到文本。
第一的:
Dim wb as new WebClient
Dim html as string = wb.DownloadString("http://stackoverflow.com")
然后,您搜索该字符串,为此您可以使用IndexOf。
我支持 Luxspes 的回答。
更多代码只是为了更有帮助。尚未对此进行测试,我希望这可行:
Dim wb As New WebClient
Dim html As String = wb.DownloadString("http://stackoverflow.com")
'To know if there are YOUR STRING inside
Dim BooleanAnswer As Boolean = html.Contains("YOUR STRING")
'To know how many instances
Dim HowMany As Integer = FindIndexes("YOUR STRING", html).Count
'To output them all through Console.Write or your preferred output(the indexes)
Dim FoundList As List(Of Integer) = FindIndexes("YOUR STRING", html)
For i As Integer = 0 to FoundList.Count - 1
Console.Write(i & "-index: " & FoundList(i).toString)
Next i
'Function
Private Function FindIndexes(ByVal searchWord As String, ByVal src As String) as List(Of Integer)
Dim searchSRC As String = src
Dim toFind As String = searchWord
Dim lastIndex As Integer = 0
Dim listOfIndexes As New List(Of Integer)
Do Until lastIndex < 0
lastIndex = searchSRC.IndexOf(toFind, lastIndex + toFind.Length)
If lastIndex >= 0 Then
listOfIndexes.Add(lastIndex)
End If
Loop
Return listOfIndexes
End Function