您需要知道如何使用 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(" ") 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(" ") Then
row(i) += itemNode.InnerText
Else
row(i) = itemNode.InnerText
i += 1
End If
Next
Next
DataGridView1.DataSource = dt
我的测试结果:
如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。
注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。