html - 通过 F5 或 F8 运行一次/两次但随后出现多个错误的代码

标签 html vba excel internet-explorer web-scraping

为了修复the following code ,我试图把它分成更小的部分。因此,我在 Sheet1 中的以下代码让我疯狂了好几个小时:

    Sub Scrapping_Data()
    Dim IE As Object, EURUSD1 As String, EURUSD2 As String
    Application.ScreenUpdating = False
    Range("A:B").Clear

    Set IE = CreateObject("internetexplorer.application")

    With IE
       .Navigate "http://uk.investing.com/currencies/streaming-forex-rates-majors"
       .Visible = False
    End With    

    Do
        DoEvents
    Loop Until IE.readyState = READYSTATE_COMPLETE

    Set FOREX = IE.document.getElementById("pair_1")
    EURUSD1 = FOREX.Cells(1).innerHTML
    EURUSD2 = FOREX.Cells(2).innerHTML
    IE.Quit
    Set IE = Nothing

    Range("A1").Value = EURUSD1
    Range("B1").Value = EURUSD2
    End Sub

我第一次运行它,效果很好。但是当我第二次运行它时,发生错误运行时错误'91'。所以我点击了F8,但什么也没发生,代码工作正常,我检查了Sheet1,Cells(1,1)Cells(1,2)中有值。然后我再次运行它,这次发生错误运行时错误'13'。我再次单击F8,但什么也没发生,代码工作正常。当我继续运行代码时,错误仍然发生,并且单击F8并不能帮助找到问题。我的代码有什么问题吗?如何解决?

我在这里也不明白的是,每次运行代码时,我的笔记本电脑都会变慢,并且我必须多次手动重新启动它。

最佳答案

The following requires that you go into the VBE's Tools ► References and place checkmarks beside Microsoft HTML Object library and Microsoft XML v6.0.

这是一个相当于将 Internet Explorer 对象网络抓取到同一 URL。

Option Explicit

Sub tournamentFixtures()
    'declare the objects with early binding
    Dim htmlBDY As New HTMLDocument, xmlHTTP As New MSXML2.XMLHTTP60
    'declare the regular variables
    Dim sURL As String, ws As Worksheet

    'set a var object to the destination worksheet
    Set ws = Worksheets("Sheet1")

    'assign the URL to a string var
    sURL = "http://uk.investing.com/currencies/streaming-forex-rates-majors"

    'isolate all commands to the MSXML2.XMLHTTP60 object
    With xmlHTTP
        'initiate the URL
        .Open "GET", sURL, False
        'set hidden header information
        .setRequestHeader "User-Agent", "XMLHTTP/1.0"
        'get the page data
        .send

        'safety check to make sure we got the web page's data
        If .Status <> 200 Then GoTo bm_safe_Exit

        'if here you got the page data - copy it to the local var
        htmlBDY.body.innerHTML = .responseText
    End With

    'localize all commands to the page data
    With htmlBDY
        'check if the element ID exists
        If Not .getElementById("pair_1") Is Nothing Then
            'it exists - get the data directly to the worksheet
            With .getElementById("pair_1")
                ws.Range("A1") = .Cells(1).innerText
                ws.Range("B1") = .Cells(2).innerText
            End With
        Else
            'it doesn't exist - bad page data
            MsgBox "there is no 'pair_1' on this page"
        End If

    End With

bm_safe_Exit:
    'clean up all of the objects that were instantiated
    Set htmlBDY = Nothing: Set xmlHTTP = Nothing: Set ws = Nothing
End Sub

我几乎对每一行都进行了评论,以便您可以了解正在发生的事情。这可能需要一些调整。我运行了大约 40 次,有一次失败了,但这可能是我自己的互联网连接问题。将此视为您可以进行自己的研究以实现目标的起点。如果您在使用此新代码时仍然遇到问题,请不要将其粘贴到另一个问题中,并询问为什么它在没有进行一些研究并亲自尝试解决方案的情况下不起作用。 StackOverflow是一个面向专业和爱好者程序员的网站。


我放弃了尝试提供网络抓取问题的解决方案,因为页面技术变化太快,无法跟上外围水平。你必须参与到即时的变化中才能快速使用react,而我自己的兴趣在别处。我回复了这个请求,因为您实际上提供了要测试的 URL(很少有人提出问题实际上认为这很重要 - 想想看),并且我认为 var 的静态调暗会有所帮助。

关于html - 通过 F5 或 F8 运行一次/两次但随后出现多个错误的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38299344/

相关文章:

VBA excel : How do I compare a string array to a string?

excel - 根据其他单元格中的符号计算单元格值的公式

html - 是否可以让 CSP 仅适用于父框架,而不适用于任何 iframe?

javascript - 防止空白链接(有 javascript :;) from openening in new tab/window on middle click. ..?

arrays - 计算单元格变化

sql - 使用日期字段的月份部分返回字符串

arrays - Excel 函数 : reference to array in cell

java - 异常读取 XLSB 文件 Apache POI java.io.CharConversionException

html - 使用 .htaccess 删除 .html URL 扩展时遇到问题

android - 如何在 Android (Nexus 7) 上显示不可缩放的网页