有没有办法找到网站上是否有文字?

时间:2012-06-17 03:16:52

标签: vb.net web

所以说我想找到“Hello,world!”这个词。在网页上。它的ID为“文本”。使用名为WebBrowser1的WebBrowser,如果该文本存在于网页上,或者如果该文本不存在,则返回true会返回false吗?无论哪种方式,URL都是完全相同的。

编辑:获取它的HTML路径很长,所以我需要在页面上找到文本。

2 个答案:

答案 0 :(得分:4)

首先:

Dim wb as new WebClient
Dim html as string = wb.DownloadString("http://stackoverflow.com")

然后,您搜索该字符串,因为您可以使用IndexOf

答案 1 :(得分:1)

我是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