首页 > 解决方案 > 使用子字符串方法从 html 中提取文本

问题描述

我想从 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 这样的子字符串方法吗?谢谢

标签: htmlvb.netsubstring

解决方案


因此,使用 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)

推荐阅读