如何从 class name inside a id name in excel VBA 获取数据?
How to get data from class name inside a id name in excel VBA?
如何从 类 date
和 number
(所有 3 个)数据中获取数据?
Sub Fii_dii()
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.nseindia.com/products/content/equities/equities/fii_dii_market_today.htm"
While IE.readyState <> 4
DoEvents
Wend
'Copy data
MsgBox IE.Document.all.Item("fiiTable").innerText
End Sub
这些值由浏览器从另一个 URI 动态检索,您可以在刷新页面时在浏览器的网络选项卡中找到该 URI。我会简单地发出一个 xmlhttp 请求(更快并且没有浏览器)并使用 css class 选择器来匹配 class 所需的节点。循环返回的 nodeList 并写出到 Excel
Option Explicit
Public Sub ScrapeValues()
Dim html As HTMLDocument, values As Object, i As Long, ws As Worksheet
Set html = New HTMLDocument
Set ws = ThisWorkbook.Worksheets("Sheet1")
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.nseindia.com/products/dynaContent/equities/equities/htms/fiiEQ.htm", False
.send
html.body.innerHTML = .responseText
End With
Set values = html.querySelectorAll(".date, .number")
For i = 0 To values.Length - 1
With ws
.Cells(i + 1, 1) = values.Item(i).innerText
End With
Next
End Sub
如果您想使用 IE,您需要测试这些元素是否存在以及定时循环,例如
Option Explicit
Public Sub ScrapeValues()
Dim ie As InternetExplorer, values As Object, i As Long, ws As Worksheet, t As Date
Const MAX_WAIT_SEC As Long = 5 '<==adjust time here
Set ie = New InternetExplorer
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ie
.Visible = True
.Navigate2 "https://www.nseindia.com/products/content/equities/equities/fii_dii_market_today.htm"
While .Busy Or .readyState <> 4: DoEvents: Wend
t = Timer
Do
Set values = .document.querySelectorAll(".date, .number")
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While values.Length = 0
If values.Length > 0 Then
For i = 0 To values.Length - 1
With ws
.Cells(i + 1, 1) = values.Item(i).innerText
End With
Next
End If
.Quit
End With
End Sub
--
参考(VBE>工具>参考):
- 微软HTML对象库
- Microsoft Internet 控件
如何从 类 date
和 number
(所有 3 个)数据中获取数据?
Sub Fii_dii()
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.nseindia.com/products/content/equities/equities/fii_dii_market_today.htm"
While IE.readyState <> 4
DoEvents
Wend
'Copy data
MsgBox IE.Document.all.Item("fiiTable").innerText
End Sub
这些值由浏览器从另一个 URI 动态检索,您可以在刷新页面时在浏览器的网络选项卡中找到该 URI。我会简单地发出一个 xmlhttp 请求(更快并且没有浏览器)并使用 css class 选择器来匹配 class 所需的节点。循环返回的 nodeList 并写出到 Excel
Option Explicit
Public Sub ScrapeValues()
Dim html As HTMLDocument, values As Object, i As Long, ws As Worksheet
Set html = New HTMLDocument
Set ws = ThisWorkbook.Worksheets("Sheet1")
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.nseindia.com/products/dynaContent/equities/equities/htms/fiiEQ.htm", False
.send
html.body.innerHTML = .responseText
End With
Set values = html.querySelectorAll(".date, .number")
For i = 0 To values.Length - 1
With ws
.Cells(i + 1, 1) = values.Item(i).innerText
End With
Next
End Sub
如果您想使用 IE,您需要测试这些元素是否存在以及定时循环,例如
Option Explicit
Public Sub ScrapeValues()
Dim ie As InternetExplorer, values As Object, i As Long, ws As Worksheet, t As Date
Const MAX_WAIT_SEC As Long = 5 '<==adjust time here
Set ie = New InternetExplorer
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ie
.Visible = True
.Navigate2 "https://www.nseindia.com/products/content/equities/equities/fii_dii_market_today.htm"
While .Busy Or .readyState <> 4: DoEvents: Wend
t = Timer
Do
Set values = .document.querySelectorAll(".date, .number")
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While values.Length = 0
If values.Length > 0 Then
For i = 0 To values.Length - 1
With ws
.Cells(i + 1, 1) = values.Item(i).innerText
End With
Next
End If
.Quit
End With
End Sub
--
参考(VBE>工具>参考):
- 微软HTML对象库
- Microsoft Internet 控件