我正在尝试使用VBA脚本excel宏link从网站下载数据,但我感兴趣的数据驻留在网页框架中,主页面的网址和框架没有得到更改
以下是适用于主页的宏,请帮我从网页框架中下载。
'Microsoft HTML Office Library
Sub webtable_NSE()
Dim HTMLDoc As New HTMLDocument
Dim objElementsTd As Object
Dim objTd As Object
Dim lRow As Long
Dim myarray()
Dim oIE As InternetExplorer
Set oIE = New InternetExplorer
oIE.navigate "https://www.nseindia.com/products/content/equities/ipos/ipo_current_quess.htm"
'javascript:loadIpoBidDetails('/products/content/equities/ipos/Quess_curr_ipo_bid_details.htm')
'oIE.Navigate "https://www.nseindia.com/products/content/equities/ipos/Quess_curr_ipo_bid_details.htm"
Do Until (oIE.readyState = 4 And Not oIE.Busy)
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:03"))
HTMLDoc.body.innerHTML = oIE.Document.body.innerHTML
With HTMLDoc.body
Set elemcollection = .getElementsByTagName("Table")
For t = 0 To elemcollection.Length - 1
For r = 0 To elemcollection(t).Rows.Length - 1
For c = 0 To elemcollection(t).Rows(r).Cells.Length - 1
ThisWorkbook.Sheets("NSE").Cells(ActRw + r + 1, c + 1) = elemcollection(t).Rows(r).Cells(c).innerText
Next c
Next r
ActRw = ActRw + elemcollection(t).Rows.Length + 1
Next t
End With
oIE.Quit
End Sub
答案 0 :(得分:1)
如果没有VBA,你可以做你想做的事。
在“数据”标签中,您可以点击“来自网络,地址:https://www.nseindia.com/products/content/equities/ipos/QUESS_curr_ipo_bid_details.htm”,然后点击“导入”。之后,您可以将连接属性更改为“打开文件时刷新数据”或控制何时使用VBA刷新数据
答案 1 :(得分:1)
如果你知道包含你想要的表格数据的网址,你就不必尝试自动化IE以“到达那里”。 - 您只需针对该特定URL发送HTTP请求即可。您正在寻找的库是MSXML2 XMLHTTP - 这是返回数据的代码示例:
Option Explicit
Sub GetTableData()
Dim objRequest As Object
Dim strUrl As String
Dim strHtml As String
Dim blnAsync As Boolean
Set objRequest = CreateObject("MSXML2.XMLHTTP")
strUrl = "https://www.nseindia.com/products/content/equities/ipos/QUESS_curr_ipo_bid_details.htm"
blnAsync = True
With objRequest
.Open "GET", strUrl, blnAsync
.Send
While .readyState <> 4
DoEvents
Wend
strHtml = .responseText
End With
Debug.Print strHtml
End Sub