【问题标题】:VBA Excel WebScrapingVBA Excel 网页抓取
【发布时间】:2021-04-30 19:58:29
【问题描述】:

我有一个项目,我需要从网络上抓取不同数量的货币值(大约 15 个)。对于一个抓取以下代码的人来说,工作正常,但我如何编写代码以咨询大量货币?

*每种货币都有一个特定的链接。

Sub currence()

    Range("A5:A16").ClearContents

    'AED
    Set ie = CreateObject("InternetExplorer.Application")
    ie.navigate "https://br.investing.com/currencies/aed-brl"

    Do While ie.busy And ie.readyState <> "READYSTATE_COMPLETE"
    DoEvents
    Loop

    Set HTML = ie.Document
    Cells(5, 1) = HTML.getElementsByClassName("top bold inlineblock")(0).innerText

    Debug.Print Cells(5, 1)
    ie.Quit

    Range("A5:A16").WrapText = False

End Sub

【问题讨论】:

  • 创建一个链接数组并循环遍历它?
  • @BigBen 是的,我考虑过,但不知道代码到底长什么样。

标签: excel vba web web-scraping


【解决方案1】:

试试这个

Sub currence()
Dim myURL() As Variant
Dim html As Object
Dim a As Object
Dim i As Integer
Dim r As Integer
Cells.Clear
Set html = CreateObject("htmlfile")
With CreateObject("MSXML2.XMLHTTP")
    .Open "GET", "https://br.investing.com/currencies", False
    .send
    html.body.innerHTML = .responseText
    For Each a In html.getElementsByClassName("bold left noWrap elp plusIconTd")
        i = i + 1
        ReDim Preserve myURL(1 To i)
        myURL(i) = Replace(a.getElementsByTagName("a")(0).href, "about:", "https://br.investing.com")
    Next a
    For j = LBound(myURL) To UBound(myURL)
        .Open "GET", myURL(j), False
        .send
        html.body.innerHTML = .responseText
        r = r + 1
        Cells(r, 1) = UCase(Split(myURL(j), "/")(4))
        Cells(r, 2) = html.getElementsByClassName("top bold inlineblock")(0).innerText
    Next j
End With

结束子

【讨论】:

  • 嗨,Ges,我不能使用这个板,因为我需要比较“BRL vs OTHER CURRENCY”。此代码返回“错误 438 对象不支持此属性或方法”。
猜你喜欢
  • 2014-11-25
  • 2020-12-05
  • 2018-05-31
  • 1970-01-01
  • 2021-12-20
  • 2017-05-10
  • 1970-01-01
  • 2013-08-27
  • 2019-04-03
相关资源
最近更新 更多