【问题标题】:Web scraping in excelexcel中的网页抓取
【发布时间】:2019-02-28 06:50:08
【问题描述】:

我想使用 vba 从“https://www.oanda.com/currency/converter/”中提取数据(例如,从美元到欧元) 我曾尝试执行以下代码..但它仍然无法正常工作

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

【问题讨论】:

  • 到目前为止您尝试了哪些方法,您遇到了哪些问题?
  • 你在哪里得到错误?
  • 消息框为空
  • 如果没有报错,我认为元素的InnerText是空的。如果我查看该网站,我还会注意到他们出售他们的 API,所以也许他们保护了他们的网站不被抓取,因为我看不到你想要的价值出现在哪里。
  • 我知道它显示了 HTML 代码,但在该 html 代码的某处,它也应该显示您想要的值。如果不是,那么您正在查看错误的元素。

标签: excel vba web-scraping


【解决方案1】:

可以从outerHTML中获取,也可以从隐藏元素的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

【讨论】:

  • 你不能立即从元素中获取 Value 属性,而不是获取 outerHTML,因为它是一个输入元素。
  • 是的,你可以。很好发现。
  • @Pratham 你在消失之前试过这个吗?
猜你喜欢
  • 1970-01-01
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
  • 2017-05-10
  • 2018-05-31
  • 2020-06-18
相关资源
最近更新 更多