使用子字符串方法从html提取文本

时间:2020-10-18 19:28:22

标签: html vb.net substring

我想从html提取文本。 我已经通过webrequest获得了html源。

如何像下面的示例一样提取文本?

class="btn btn-success btn-lg" href="I wanto to get this link that is changing every time" rel="nofollow noopener">Click</a><

我可以使用子字符串方法(例如startwith和end with)吗? 谢谢

1 个答案:

答案 0 :(得分:2)

因此,我使用string.indexof找到了一个解决方案。 html字符串中的那些“”让我有点挣扎,但这现在可以完成它应该做的事情。

我找到了解决方法!

 Dim allinputtext As String = RichTextBox1.Text
    Dim textafter As String = """ rel=""nofollow noopener"
    Dim textbefore As String = "class=""btn btn-success btn-lg"" href="""
    Dim startPosition As Integer = allInputText.IndexOf(textBefore)

    'If text before was not found, return Nothing
    If startPosition < 0 Then

    End If

    'Move the start position to the end of the text before, rather than the beginning.
    startPosition += textBefore.Length

    'Find the first occurrence of text after the desired number
    Dim endPosition As Integer = allInputText.IndexOf(textAfter, startPosition)

    'If text after was not found, return Nothing
    If endPosition < 0 Then

    End If

    'Get the string found at the start and end positions
    Dim textFound As String = allInputText.Substring(startPosition, endPosition - startPosition)
    TextBox4.Text = (textFound)