简体   繁体   English

Excel Web查询动态数据

[英]Excel web query for dynamic data

I am trying to pull mortgage rates from a bank website into Excel 2010. This is the applicable website: http://www.bmo.com/home/personal/banking/rates/mortgages 我试图将抵押贷款利率从银行网站提取到Excel 2010中。这是适用的网站: http : //www.bmo.com/home/personal/banking/rates/mortgages

I have created a web query in Excel to do this: (Data --> From Web --> "web site"). 我已经在Excel中创建了一个Web查询来执行此操作:(数据->从Web->“网站”)。 Looking through a browser, the page displays actual rates. 通过浏览器查看,该页面显示实际汇率。 In Excel, I only get placeholders like this: 在Excel中,我只能得到这样的占位符:

Mortgage Rates  
5 year Low Rate (closed)   %*
Prime Rate                 %

The problem seems to be that Excel does not recognize the dynamic content on the webpage. 问题似乎是Excel无法识别网页上的动态内容。 If I "view source" for the data of interest, I see that the rates are populated by javascript variables: 如果我“查看源”以获取感兴趣的数据,则可以看到这些费率是由javascript变量填充的:

<td class="ratesSubLabel">Prime Rate</td>
<td class="rate"><script type="text/javascript">document.write(prime.value);</script>%</td>

Can anyone help with this? 有人能帮忙吗?

I've never really used the web source type thing before (it never worked how I wanted). 之前我从未真正使用过Web源代码类型的东西(它从未按我想要的方式工作)。

You can use VBA to scrape the data from the page into Excel. 您可以使用VBA将页面中的数据抓取到Excel中。

The following should read the data from the table on the site and print the results into sheet1. 以下应从站点上的表中读取数据,并将结果打印到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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM