Excel中的Web抓取

时间:2019-02-28 06:50:09

标签: excel vba web-scraping

我想使用vba在excel中从“ https://www.oanda.com/currency/converter/”提取数据(例如,从USD转换为EUR) 我曾尝试执行以下代码..但仍无法正常工作

Sub PullData()

    Dim IE As Object
    Dim doc As HTMLDocument

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True

    IE.Navigate "https://www.oanda.com/currency/converter/"

    Do While IE.Busy Or IE.ReadyState <> 4
        Application.Wait DateAdd("s", 1, Now)
    Loop

    Set doc = IE.Document  

    doc.getElementById("quote_currency_input").Value = "EURO"
    doc.getElementById("base_currency_input").Value = "US Dollar"
           Do While IE.Busy Or IE.ReadyState <> 4
        Application.Wait DateAdd("s", 1, Now)
    Loop

    MsgBox doc.getElementsByClassName("want").Item(0).innerText

    IE.Quit

End Sub

1 个答案:

答案 0 :(得分:0)

您可以从externalHTML或隐藏元素的value属性中获取

Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub GetData()
    Dim ie As New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "https://www.oanda.com/currency/converter/"

        While .Busy Or .readyState < 4: DoEvents: Wend

       .document.getElementById("quote_currency_input").Value = "EURO"
       .document.getElementById("base_currency_input").Value = "US Dollar"
        Debug.Print .document.querySelector("#form_base_amount_input_hidden").Value
        Debug.Print Split(Split(.document.getElementById("form_base_amount_input_hidden").outerHTML, "value=" & Chr$(34))(1), Chr$(34))(0)
        .Quit
    End With
End Sub