从 investing.com 读取的 HtmlAgilityPack 数据

Hui Liu-MSFT 48,571 信誉分 Microsoft 供应商
2024-03-22T09:10:16.3966667+00:00

嗨, 我需要帮助从以下 URL https://www.investing.com/equities/wrldcal-teleco-earnings 读取 html 表(EPS 收入表),并将我的代码插入数据表中,因为它的连接和投资返回数据,因为我对 htmlaglibitypack 的使用一无所知。80343-tabledata.jpg

我的代码如下 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Using client As New WebClient() client.Headers.Add(“user-agent”, “karen payne”) ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType) ServicePointManager.DefaultConnectionLimit = 9999 Dim page As String = client.DownloadString(siteAddress) ' TextBox1.Text = htmlCode

        '  Dim page As String = WebClient.DownloadString("https://www.investing.com/equities/wrldcal-teleco")  
        Dim doc As HtmlAgilityPack.HtmlDocument = New HtmlAgilityPack.HtmlDocument()  
        doc.LoadHtml(page)  

        Dim lstNode As List(Of HtmlNode) = New List(Of HtmlNode)  
        Dim lstName As List(Of String) = New List(Of String)  
        Dim lstTable As List(Of DataTable) = New List(Of DataTable)  



        For Each thed As HtmlNode In doc.DocumentNode.SelectNodes("//thead")  
            lstNode.Add(thed.ParentNode)  
            Dim text = thed.SelectSingleNode("tr").SelectSingleNode("th").InnerText  
            '  Console.WriteLine(text)  
            MsgBox(text)  

            MsgBox(text)  
            lstName.Add(text)  
        Next  


        For i As Integer = 0 To lstNode.Count - 1  
            Dim dt As DataTable = New DataTable  
            dt.TableName = lstName(i)  
            For Each trNode As HtmlNode In lstNode(i).SelectNodes("tr")  
                If trNode.Attributes("class") Is Nothing Then  
                    For Each colNode As HtmlNode In trNode.SelectNodes("td")  
                        dt.Columns.Add(colNode.InnerText)  
                    Next  
                Else  
                    Dim j As Integer = 0  
                    Dim row As DataRow = dt.NewRow()  
                    For Each rowNode As HtmlNode In trNode.SelectNodes("td")  
                        row(j) = rowNode.InnerText  
                        j += 1  
                    Next  
                    dt.Rows.Add(row)  
                    '  MsgBox("test")  
                End If  
            Next  
            lstTable.Add(dt)  
            DataGridView1.DataSource = dt  

        Next  
    End Using  

    MsgBox("final finished...")  



End Sub  
 



Note:此问题总结整理于:HtmlAgilityPack Data read from investing.com

VB
VB
Microsoft 开发的一种面向对象的编程语言,其在 .NET Framework 上实现。 以前称为 Visual Basic .NET。
73 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Jiale Xue - MSFT 46,296 信誉分 Microsoft 供应商
    2024-03-22T09:32:25.4666667+00:00

    您需要知道如何使用 Html Agility Pack 获取特定表格。 请看以下代码:

                ...  
                Dim lstName As List(Of String) = New List(Of String)  
                Dim dt As DataTable = New DataTable  
      
                For Each table As HtmlNode In doc.DocumentNode.SelectNodes("//table[contains(@id,'earningsHistory')]")  
                    For Each columnNameNode As HtmlNode In table.SelectNodes(".//tr/th")  
                        Dim columnName As String = columnNameNode.InnerText  
                        If columnName.Contains("&nbsp") Then  
                            Dim name = columnName.Replace(" ", " ")  
                            lstName(lstName.Count - 1) += name  
                        Else  
                            lstName.Add(columnName)  
                        End If  
                    Next  
                    For Each colName As String In lstName  
                        dt.Columns.Add(colName)  
                    Next  
                    Dim i As Integer  
                    Dim row As DataRow = dt.NewRow()  
                    For Each itemNode As HtmlNode In table.SelectNodes(".//tr/td")  
      
                        If i = dt.Columns.Count Then  
                            i = 0  
                            dt.Rows.Add(row)  
                            row = dt.NewRow()  
                        End If  
                        If itemNode.InnerText.Contains("&nbsp") Then  
                            row(i) += itemNode.InnerText  
                        Else  
                            row(i) = itemNode.InnerText  
                            i += 1  
                        End If  
                    Next  
                Next  
      
                DataGridView1.DataSource = dt  
    

    我的测试结果: 80888-1.png


    如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。

    注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。

    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。