gpt4 book ai didi

excel - 如何访问表中的数据?

转载 作者:行者123 更新时间:2023-12-04 20:52:37 26 4
gpt4 key购买 nike

表格1:
Table1
表2:
Table2
我正在尝试从网站上的表格中抓取数据。
有两个表具有相同的表类名称,但不同的 id。
我需要从 ID 为“option”的表中提取数据。

Sub hsiotpions()
Dim ieObj As InternetExplorer
Dim htmlEle As IHTMLElement
Dim i As Integer

i = 1

Set ieObj = New InternetExplorer
ieObj.Visible = False
ieObj.navigate "https://www.hkex.com.hk/Market-Data/Futures-and-Options-Prices/Equity-Index/Hang-Seng-Index-Futures-and-Options?sc_lang=en#&product=HSI"

Application.Wait Now + TimeValue("00:00:05")

For Each htmlEle In ieObj.document.getElementById("option").getElementByTagName("tr")
With ActiveSheet
.Range("A" & i).Value = htmlEle.Children(0).textContent
.Range("B" & i).Value = htmlEle.Children(1).textContent
.Range("C" & i).Value = htmlEle.Children(2).textContent
.Range("D" & i).Value = htmlEle.Children(3).textContent
.Range("E" & i).Value = htmlEle.Children(4).textContent
.Range("F" & i).Value = htmlEle.Children(5).textContent
.Range("G" & i).Value = htmlEle.Children(6).textContent
.Range("H" & i).Value = htmlEle.Children(7).textContent
.Range("I" & i).Value = htmlEle.Children(8).textContent
.Range("J" & i).Value = htmlEle.Children(9).textContent
.Range("K" & i).Value = htmlEle.Children(10).textContent
.Range("L" & i).Value = htmlEle.Children(11).textContent
End With

i = i + 1

Next htmlEle

End Sub

最佳答案

您可以使用 id 来选择表。请务必正确处理第 4 列和第 8 列,以便将其写入工作表。

在您的代码中,您有一个错误,您不使用复数:

getElementByTagName("tr")

那应该是
getElementsByTagName("tr")

它返回一个集合。

表体的行实际上具有相同的类名,我使用下面的行来循环行。

我不喜欢硬编码的等待时间,但是由于我目前看不到页面完全加载时的良好指标,因此您可以调整以下行,直到获得所有行
Application.Wait Now + TimeSerial(0, 0, 10)

end 参数是以秒为单位的等待时间。
Option Explicit    
Public Sub GetTable()
Dim ws As Worksheet, ie As Object, table As Object, headers()
Dim headersTop As Object, ele As Object

Set ws = ThisWorkbook.Worksheets("Sheet1")
Set ie = CreateObject("InternetExplorer.Application")
headers = Array("OI", "Volume", "IV", "Bid/Ask", "Last", "Strike", "Last", "Bid/Ask", "IV", "Volume", "OI") '<== This is second row of headers

With ie
.Visible = True
.Navigate2 "https://www.hkex.com.hk/Market-Data/Futures-and-Options-Prices/Equity-Index/Hang-Seng-Index-Futures-and-Options?sc_lang=en#&product=HSI"

While .Busy Or .readyState < 4: DoEvents: Wend

Application.Wait Now + TimeSerial(0, 0, 10)

Set table = .document.querySelector("#option")
Set headersTop = .document.querySelectorAll("#option tr:first-child th") '<== This is top row of headers which involves merged table cells. I prepare the excel sheet in the same way in the code below.
ws.Range("A1:D1").Merge
ws.Range("A1").Value = headersTop.item(0).innerText ' CALL
ws.Range("E1:G1").Merge
ws.Range("E1") = headersTop.item(1).innerText '< Date
ws.Range("H1:K1").Merge
ws.Range("H1") = headersTop.item(2).innerText '< PUT
ws.Cells(2, 1).Resize(1, UBound(headers) + 1) = headers
Dim r As Long, c As Long, td As Object, tr As Object
r = 3
For Each tr In table.getElementsByClassName("tdrow") 'loop the rows below the headers by using class name to isolate
c = 1
For Each td In tr.getElementsByTagName("td") '< loop table cells i.e. columns of rows
ws.Cells(r, c) = IIf(c Mod 4 = 0, "'" & td.innerText, td.innerText) '< If column number is 4 or 8 then add "'" in front so formatting preserved
c = c + 1
Next
r = r + 1
Next
.Quit
End With
End Sub

CSS 选择器:

我正在使用 css selectors最初。现代浏览器针对使用 css(管理页面样式)进行了优化。我们通过 querySelector 应用 css 选择器和 querySelectorAll 在这种情况下, ie.document 的方法.

这是 #option tr:first-child th正在选择:



您可以看到它是顶级标题。由于表格中实际上有比这更多的列,我们知道我们已经合并了单元格。

由于我们想要多个标题,我们使用 querySelectorAll方法,而不是 querySelector返回单个元素,返回 nodeList由 ("") 中的选择器指定的匹配元素。然后通过从 0 开始的索引访问 nodeList 中的项目。

表体:

我已经通过 id 检索了感兴趣的表
Set table = .document.querySelector("#option")

# 是一个 css id 选择器,在语义上等价于:
Set table = .document.getElementById("option")

只是,除了非常旧的 IE 版本之外,css 选择器方法应该会更快。

当我们有表时,我们知道行是 tr元素和 td是表格单元格(列)。

查看我们的 table 中的表格行变量你看到我们有两个不同长度的标题:



这就是为什么我选择不循环这些试图写出的原因。底行的值是静态的,所以我将它们放在数组变量 headers 中写出来。第一行有一个动态的日期值,所以我使用 css 选择器自行检索该行:
Set headersTop = .document.querySelectorAll("#option tr:first-child th")

并索引到返回的 nodeList 以将项目放置在一个网页布局所指示的各种合并单元格中。

但请注意,表体行都具有相同的类名:



因此,我通过在表行上使用类名来选择这些:
For Each tr In table.getElementsByClassName("tdrow")

然后我循环这些行中的列:
For Each td In tr.getElementsByTagName("td")

因为第 4 列和第 8 列的格式 Excel 将尝试将您作为日期/分数处理,您想在前面添加“'”以保留格式:

enter image description here
ws.Cells(r, c) = IIf(c Mod 4 = 0, "'" & td.innerText, td.innerText)

关于excel - 如何访问表中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54868200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com