【问题标题】:IE fast automationIE快速自动化
【发布时间】:2016-09-04 05:09:29
【问题描述】:

我目前正在尝试使用 VBA 从一个名为 X-Rates 的网站上抓取一些汇率。我目前的问题是运行时间太长。我已将其范围缩小到我的 IE 对象的 Do Events

我的问题:有没有更好的方法(也许代码效率更高),或者我的逻辑有缺陷?

代码的作用如下:

1 - 每个国家的循环 (1-9 = offsetCurr);

2- 转换为汇率并在单元格上保存价值

    'Define variables
    Dim strElm     As String
    Dim i          As Integer
    Dim ie         As InternetExplorer
    Dim period     As Variant
    Dim offsetCurr As Integer
    Dim offsetDesc As String

    'Define period
    period = Application.InputBox("What's the year and period?", "Period", , , , , 2)

    'Define start row
    i = 2

    Application.ScreenUpdating = False

    On Error GoTo ErrHandler

    For offsetCurr = 1 To 9

        If offsetCurr = 1 Then

            'ARS to EURO
            Set ie = New InternetExplorer
            offsetDesc = "ARS"
            Cells(i, 1).Value = period
            Cells(i, 2).Value = offsetDesc
            ie.navigate "http://www.x-rates.com/calculator/?from=ARS&to=EUR&amount=1"

            Do While ie.readyState <> READYSTATE_COMPLETE
                DoEvents
            Loop

            d = ie.document.getElementsByClassName("ccOutputRslt")(0).innerText
            strElm = d
            Cells(i, 3).Value = strElm

            ie.Quit
            Set ie = Nothing

            'ARS to USD
            Set ie = New InternetExplorer
            ie.navigate "http://www.x-rates.com/calculator/?from=ARS&to=USD&amount=1"

            Do While ie.readyState <> READYSTATE_COMPLETE
                DoEvents
            Loop

            d = ie.document.getElementsByClassName("ccOutputRslt")(0).innerText
            strElm = d
            Cells(i, 4).Value = strElm

            'Quit IE for automation purposes
            ie.Quit
            Set ie = Nothing

            'ARS to GBP
            Set ie = New InternetExplorer
            ie.navigate "http://www.x-rates.com/calculator/?from=ARS&to=GBP&amount=1"

            Do While ie.readyState <> READYSTATE_COMPLETE
                DoEvents
            Loop

            d = ie.document.getElementsByClassName("ccOutputRslt")(0).innerText
            strElm = d
            Cells(i, 5).Value = strElm

            ie.Quit
            Set ie = Nothing

        End If

ErrHandler:

    If Err.Number <> 0 Then
        Msg = "Error #" & Str(Err.Number) & " was generated by " & Err.Source & "." & Chr(13) & "Error description: " & Err.Description
        MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
        Exit Sub
    End If

End Sub

我知道代码量很大,如果需要,我可以编辑问题以使其更简单。

【问题讨论】:

  • 我在这个问题 (stackoverflow.com/questions/27626253/…) 上发现了一种解决方法可能是“退出时删除浏览历史记录”。将尝试实施
  • 所以你是说 DoEvents 部分很慢?如果您在等待 DoEvents 而不是在浏览器上等待 VBA。您还可以打开 3 个单独的浏览器,这样您就不必从一个浏览器来回导航 9 次。
  • 如果您的工作代码只需要改进,那么您可能在这篇文章的错误位置。 Code Review 是他们处理现有/工作代码的地方,并在速度、安全性、可持续性和寿命方面尽最大努力改进它。试一试。他们很好!
  • 你试过用 XHR like this 代替 IE 吗?
  • @omegastripes 不,我没有。会尝试 !!谢谢!

标签: regex vba excel internet-explorer web-scraping


【解决方案1】:

这是一个展示如何通过 XHR 检索费率的示例:

Option Explicit

Sub TestGetRate()

    Dim sCrcy As Variant

    For Each sCrcy In Array("EUR", "USD", "GBP")
        Debug.Print GetRate("ARS", sCrcy)
    Next

End Sub

Function GetRate(sFromCrcy, sToCrcy)

    Dim sUrl, sContent

    sUrl = "http://www.x-rates.com/calculator/?from=" & sFromCrcy & "&to=" & sToCrcy & "&amount=1"
    With CreateObject("MSXML2.XMLHttp")
        .Open "GET", sUrl, False
        .send
        sContent = .ResponseText
    End With
    With CreateObject("VBScript.RegExp")
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = "<span class=""ccOutputRslt"">(.*?)<span class=""ccOutputTrail"">(.*?)</span><span class=""ccOutputCode"">(.*?)</span></span>"
        With .Execute(sContent).Item(0)
            GetRate = .SubMatches(0) & .SubMatches(1) & .SubMatches(2)
        End With
    End With

End Function

我的输出如下:

0.061688 欧元
0.070373 美元
0.048865 英镑

【讨论】:

  • @svacxpython 您可以在 MSDN 甚至 Wikipedia 上找到有关 XMLHTTPRequest 方法和属性的信息,您真正需要的是在真实网站上进行一些练习。尝试 Chrome - 开发者工具 (F12) - 网络选项卡,在网页上进行一些操作并找到出现的 XHR,检查标题、查询参数和数据。大多数网页抓取意味着一些逆向工程和网页结构审查。
【解决方案2】:

看起来您正在启动一个新的 IE 实例,并为 9 个循环中的每一个循环完全关闭它。尝试在开始时启动一次 IE,然后循环遍历每种货币类型,然后退出 IE。

【讨论】:

  • 刚刚这样做了,但是浏览器一直卡住!是否推荐使用其他浏览器?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2019-06-05
  • 1970-01-01
相关资源
最近更新 更多