vba 从 iframe 中按 ID 获取元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41917779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Element by ID from inside an iframe
提问by Edward Murray
I'm trying to copy the last price of share prices into an Excel worksheet from
我正在尝试将股票价格的最后价格复制到 Excel 工作表中
<div id="last-price-value" vkey="LastPrice">127.36</div>.
<div id="last-price-value" vkey="LastPrice">127.36</div>.
Internet Explorer opens and navigates to the site but the last price isn't copied to my worksheet. I added Debug.Print
element.innertext but nothing appears in the immediate pane.
Internet Explorer 打开并导航到该站点,但最后的价格未复制到我的工作表中。我添加了Debug.Print
element.innertext 但没有出现在直接窗格中。
Dim element As IHTMLElement
Dim elements As IHTMLElementCollection
Dim ie As New InternetExplorer
Dim html As HTMLDocument
my_page = "http://www.morningstar.com/stocks/XASX/COH/quote.html"
With ie
.Visible = TRUE
.Navigate my_page
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Set html = ie.document
Set element = html.getElementById("last-price-value")
Debug.Print element.innertext
Sheets("Sheet 1").Range("A2").Value = element.innertext
End With
回答by TheSilkCode
So the issue is that the last-price-value element is contained within an iFrame- iFrames are a little bit of a pain and require special syntax and a few extra steps to retrieve their data- Basically you want to find the iFrame, get its HTML using .ContentDocument
, and then can use that HTML document just like you normally would... however, this case of extracting data from an iFrame is particularly painful because the main web page is on one domain where the iFrame is on another domain, preventing you from accessing the iFrame's HTML from the main page... SO, what you have to do instead is:
1- Load the main page
2- Find the frame
3- Get the frame's URL
4- Load the frame URL
5- Pull the last-price-value..
所以问题是最后一个价格值元素包含在 iFrame 中 - iFrame 有点麻烦,需要特殊的语法和一些额外的步骤来检索它们的数据 - 基本上你想找到 iFrame,得到它的HTML 使用.ContentDocument
,然后可以像往常一样使用该 HTML 文档……但是,这种从 iFrame 中提取数据的情况特别痛苦,因为主网页位于一个域中,而 iFrame 位于另一个域中,这会阻止您从主页访问 iFrame 的 HTML... 所以,你必须做的是: 1- 加载主页 2- 找到框架 3- 获取框架的 URL 4- 加载框架 URL 5- 拉最后一个-价格值..
See code example below:
请参阅下面的代码示例:
Public Sub sampleCode()
Dim IE As New InternetExplorer
Dim HTMLDoc As HTMLDocument
Dim parentURL As String
Dim frames As IHTMLElementCollection
Dim frameURL As String
Dim frameHTML As HTMLDocument
Dim fCounter As Long
Dim targetElement As HTMLObjectElement
parentURL = "http://www.morningstar.com/stocks/XASX/COH/quote.html"
'1- Load the main page
With IE
.Visible = False
.Navigate parentURL
While .Busy Or .readyState <> READYSTATE_COMPLETE: Wend
Set HTMLDoc = IE.document
End With
'2- Find the frame
Set frames = HTMLDoc.getElementsByTagName("iframe")
'Loop through all the frames
For fCounter = 0 To frames.Length - 1
'Test each frame's ID to find the correct one (numerical appendix to ID can change so need to test for pre-fix)
If Left(frames(fCounter).ID, 10) = "QT_IFRAME_" Then
'3- Get the frame's URL
frameURL = frames(fCounter).src
Exit For
End If
Next
'4- Load the frame URL
With IE
.Navigate frameURL
While .Busy Or .readyState <> READYSTATE_COMPLETE: Wend
Set frameHTML = IE.document
End With
'Find and pull the last price
Set targetElement = frameHTML.getElementById("last-price-value")
Sheets("Sheet 1").Range("A2").Value = CDbl(targetElement.innerText)
IE.Quit
Set IE = Nothing
End Sub
Also, Yahoo has pretty easy VBA interface to pull stock prices so unless you need Morningstar because you are pulling prices on obscure mutual funds or private funds I would say you could make it easier on yourself by switching to Yahoo sources..
此外,雅虎有非常简单的 VBA 界面来拉动股票价格,所以除非你需要晨星,因为你正在拉动不起眼的共同基金或私人基金的价格,否则我会说你可以通过切换到雅虎资源来让自己更容易。
Hope this helps, TheSilkCode
希望这会有所帮助,TheSilkCode