我正在尝试将抵押贷款利率从银行网站提取到Excel 2010.这是适用的网站:http://www.bmo.com/home/personal/banking/rates/mortgages
我在Excel中创建了一个Web查询来执行此操作:(数据 - >来自Web - >“网站”)。浏览浏览器,页面显示实际费率。在Excel中,我只获得这样的占位符:
Mortgage Rates
5 year Low Rate (closed) %*
Prime Rate %
问题似乎是Excel无法识别网页上的动态内容。如果我为感兴趣的数据“查看源代码”,我会看到费率由javascript变量填充:
<td class="ratesSubLabel">Prime Rate</td>
<td class="rate"><script type="text/javascript">document.write(prime.value);</script>%</td>
任何人都可以帮忙吗?
答案 0 :(得分:2)
我之前从未真正使用过Web源类型的东西(它从来没有按我想要的方式工作)。
您可以使用VBA将页面中的数据扫描到Excel中。
以下内容应从网站上的表格中读取数据并将结果打印到sheet1。
' The following code should be put in a new code module
' Requires "Microsoft Internet Controls" and "Microsoft HTML Object Library" References
Sub Main()
Dim Browser As InternetExplorer
Dim Document As HTMLDocument
Dim Element As IHTMLElement
Dim Elements As IHTMLElementCollection
Dim Child As IHTMLElement
Dim Children As IHTMLElementCollection
Dim Row As Integer
Dim Column As Integer
Row = 1
Set Browser = New InternetExplorer
Browser.Navigate "www.bmo.com/home/personal/banking/rates/mortgages"
Do While Browser.Busy And Not Browser.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
Set Document = Browser.Document
Set Elements = Document.getElementsByTagName("table")(0).getElementsByTagName("tr")
Debug.Print Elements.Length
For Each Element In Elements
Column = 1
'Dim Children As IHTMLElementCollection
'Dim Child As IHTMLElement
Set Children = Element.getElementsByTagName("td")
For Each Child In Children
ThisWorkbook.Worksheets("Sheet1").Cells(Row, Column).Value = Child.innerText
Column = Column + 1
Next Child
Row = Row + 1
Next Element
End Sub