将Web表数据转换为excel

时间:2017-03-01 16:19:54

标签: vba

我想通过vba将“https://investing.com/indices/us-spx-500-historical-data”的月度数据转换为excel。

以下是Time Frame下拉菜单的源代码: -

<select id="data_interval" class="newInput selectBox float_lang_base_1">
        <option value="Daily" selected="">Daily</option>
        <option value="Weekly">Weekly</option>
        <option value="Monthly">Monthly</option>
    </select>enter code here

这是表源代码:

<table class="genTbl closedTbl historicalTbl" id="curr_table" tablesorter="">enter code here

这是我正在使用的VBA代码,

Sub GetData()
Dim IE As Object
Dim Links
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate ("https://uk.investing.com/indices/us-spx-500-historical-data")
IE.Visible = True
Do
    DoEvents
Loop Until IE.readyState = 4
Set Links = IE.document.getElementById("data_interval")
Links.selectedIndex = 2
End Sub

2是每月索引编号,它可以从下拉菜单中选择每月,但表格不会从每周更新到每月,我也想将此数据转换为excel

1 个答案:

答案 0 :(得分:1)

尝试这样,它可能适合您,具体取决于您的Internet Explorer。

Option Explicit

Sub GetData()

    Dim IE      As Object
    Dim Links   As Object
    Dim evt     As Object

    Set IE = CreateObject("InternetExplorer.Application")
    IE.navigate ("https://uk.investing.com/indices/us-spx-500-historical-data")
    IE.Visible = True
    Do
        DoEvents        
    Loop Until IE.readyState = 4

    Set Links = IE.document.GetElementById("data_interval")
    Set evt = IE.document.createevent("htmlevents")
    evt.initevent "change", True, False

    Links.Focus
    Links.SelectedIndex = 2
    Links.FireEvent ("onChange")

    Links.dispatchevent evt

End Sub

致使these guys正确解雇事件。