VB:将特定 HTML 标签中的文本拉入文本框

VB: Pull text from specific HTML tag into Textbox

我正在尝试编写一个需要从特定标签中提取文本的 VB.Net 应用程序:

<span data-reactid="85">172,890,000</span>

然后将 172,890,000 找到的文本输入到表单的文本框中。

Textbox1中输入要搜索的股票代号。

“TTM - 总收入”的数据将始终保存在:

<span data-reactid="85">172,890,000</span> 标签。不管你查什么库存。

RichTextBox1中,是url.

的下载源代码

TextBox2 是它拉“TTM”的地方。我可能会将其更改为标签,因为它是一个常量值。我不能将数字放入变量中,因为它会因公司而异,即输入 TextBox1.

的值

TextBox3 将显示我真正需要的值。 172,890,000举办于

<span data-reactid="85">172,890,000</span> 标签。

我想知道如何在 RichTextBox1 中搜索字符串,并在字符串结尾后拉出接下来的 7 个字符是否可行?

到目前为止我的代码是:

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim Source As String
        Dim ttm1 As String
        Dim ttmrev As String

        Source = New System.Net.WebClient().DownloadString("https://finance.yahoo.com/quote/" + TextBox1.Text + "/financials?p=" + TextBox1.Text)
        ttm1 = <span data-reactid="65">ttm</span>
        ttmrev = <span data-reactid="85"></span>

        RichTextBox1.Text = Source


        If RichTextBox1.Find(ttm1) Then

            TextBox2.Text = "ttm".ToUpper

        End If

    End Sub

End Class

和编码一样,总是有很多方法可以实现相同的最终结果。一种方法是使用正则表达式。

例如:

Imports System.Text.RegularExpressions

Sub Main

    ' In place of a static string here, place the exact line you want to search instead.
    Dim str As String = "<span data-reactid=""85"">172,890,000</span>"

    ' Search for the match
    showMatch(str, "(\d+),?(\d+),?(\d+)")

End Sub

Sub showMatch(ByVal text As String, ByVal expr As String)
    
    ' Make sure you're expression looks the way you want it
    Debug.WriteLine("The Expression: " + expr)
    
    ' Declare the variable and do the regex match
    Dim mc As MatchCollection = Regex.Matches(text, expr)
    Dim m As Match

    ' Handle the result however you wish; for example instead of a loop, since 
    ' there should only be 1 result you could just do
    ' something like: TextBox3.Text = m.ToString()
    For Each m In mc
        Debug.WriteLine(m)
    Next m
    
End Sub

上面的正则表达式将找到任何匹配的数字,例如:

172,890,000
890,000
000
2,80,000

etc.

另一种选择可能是使用 WebBrowser 控件,将源代码放入其中,然后根据需要使用 innerHtml