Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 通过F5或F8工作一次/两次但随后出现多个错误的代码_Html_Vba_Excel_Internet Explorer_Web Scraping_Xmlhttprewuest_Xmlhttprequest - Fatal编程技术网

Html 通过F5或F8工作一次/两次但随后出现多个错误的代码

Html 通过F5或F8工作一次/两次但随后出现多个错误的代码,html,vba,excel,internet-explorer,web-scraping,xmlhttprewuest,xmlhttprequest,Html,Vba,Excel,Internet Explorer,Web Scraping,Xmlhttprewuest,Xmlhttprequest,为了修复,我试着把它分成一个小部分。因此,我有以下代码,让我在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

为了修复,我试着把它分成一个小部分。因此,我有以下代码,让我在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,在
单元格(1,1)
单元格(1,2)
中有值。然后我再次运行它,这次出现了错误运行时错误'13'。我再次点击F8,但什么也没发生代码运行良好。当我继续运行代码时,错误仍然发生,单击F8也无助于发现问题。我的代码有什么问题?如何修复它


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

构建和销毁计算机需要时间;即使是在最快的系统上,也最多只需几秒钟。您可以等待适当的时间,让它放弃已加载的所有.dll等,或者您可以将IE声明为静态对象,在随后重新运行子过程时将其重新使用

Option Explicit

Sub Scrapping_Data()
    Static IE As Object
    Dim EURUSD1 As String, EURUSD2 As String

    Application.ScreenUpdating = False
    With Worksheets("Sheet1")   'KNOW what worksheet you are on!!!!!
        .Range("A:B").Clear
    End With

    If IE Is Nothing Then
        Set IE = CreateObject("internetexplorer.application")
        With IE
            .Visible = True
            '.Visible = False
            .Silent = True
        End With
    End If

    With IE
       .Navigate "http://uk.investing.com/currencies/streaming-forex-rates-majors"
       Do While .ReadyState <> 4: DoEvents: Loop
       With .document.getElementById("pair_1")
            EURUSD1 = .Cells(1).innerHTML
            EURUSD2 = .Cells(2).innerHTML
       End With
    End With

    With Worksheets("Sheet1")   'KNOW what worksheet you are on!!!!!
        .Range("A1") = EURUSD1
        .Range("B1") = EURUSD2
    End With

    IE.Navigate "about:blank"

End Sub
选项显式
子报废_数据()
静态IE作为对象
将1欧元作为字符串,将2欧元作为字符串
Application.ScreenUpdating=False
使用工作表(“Sheet1”)“了解您的工作表!!!!!”!!!!!
.范围(“A:B”).清除
以
如果我什么都不是
设置IE=CreateObject(“internetexplorer.application”)
与IE
.Visible=True
'.Visible=False
.Silent=True
以
如果结束
与IE
.导航“http://uk.investing.com/currencies/streaming-forex-rates-majors"
执行While.ReadyState 4:DoEvents:Loop
With.document.getElementById(“pair_1”)
欧元1=.Cells(1).innerHTML
EURUSD2=.Cells(2).innerHTML
以
以
使用工作表(“Sheet1”)“了解您的工作表!!!!!”!!!!!
.范围(“A1”)=1欧元
.范围(“B1”)=2欧元/美元
以
即,导航“关于:空白”
端接头
这里需要注意的是,您必须在将来的某个时候自己销毁InternetExplorer对象。关闭工作簿将关闭VBA项目,但使IE对象处于“孤立”状态

考虑到该网页附带的所有HTML5碎片,您是否考虑迁移到?如果你想知道,那么是的,这将是一个新的问题,在一组不同的[标签]

以下要求您进入VBE的工具► 在Microsoft HTML对象库Microsoft XML v6.0旁边引用并放置复选标记

这相当于将Internet Explorer对象web刮取到同一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
选项显式
子锦标赛Fixtures()
'声明具有早期绑定的对象
Dim htmlBDY作为新的HTMLDocument,xmlHTTP作为新的MSXML2.XMLHTTP60
'声明正则变量
以字符串形式标注sURL,以工作表形式标注ws
'将var对象设置为目标工作表
设置ws=工作表(“表1”)
'将URL分配给字符串变量
苏尔=”http://uk.investing.com/currencies/streaming-forex-rates-majors"
'将所有命令隔离到MSXML2.XMLHTTP60对象
使用xmlHTTP
'启动URL
.打开“GET”,sURL,False
'设置隐藏的标题信息
.setRequestHeader“用户代理”、“XMLHTTP/1.0”
'获取页面数据
.发送
“进行安全检查,确保我们获得了网页数据
如果状态为200,则转到bm_安全出口
'如果这里有页面数据,请将其复制到本地变量
htmlBDY.body.innerHTML=.responseText
以
'将所有命令本地化到页面数据
使用HTMLDY
'检查元素ID是否存在
如果不是,那么getElementById(“pair_1”)什么都不是
'它存在-直接将数据获取到工作表
带有.getElementById(“pair_1”)
ws.Range(“A1”)=.Cells(1).innerText
ws.Range(“B1”)=.Cells(2).innerText
以
其他的
'它不存在-页面数据不正确
MsgBox“此页面上没有'pair_1'”
如果结束
以
bm_安全出口:
'清除所有实例化的对象
Set htmlBDY=Nothing:Set xmlHTTP=Nothing:Set ws=Nothing
端接头
我已经评论了几乎每一行,所以你可以跟踪正在发生的事情。这可能需要一些调整。我运行了40次,有一次失败,但那可能是我自己的互联网连接。考虑这是一个起点,你可以通过自己的研究来完成你的目标。如果您对这段新代码仍然存在问题,请不要将其粘贴到另一个问题中,并询问为什么在您自己没有做一些研究和尝试解决方案的情况下它不起作用。是一个专业和热心程序员的网站



我放弃了为网页抓取问题提供解决方案的尝试,因为网页技术的变化太快,跟不上外围设备的发展。你必须参与到即时的变化中,以便能够快速响应它们,而我自己的利益就在别处。我回应了这个请求,因为你实际上提供了要测试的URL(一些提问的人实际上认为很重要的东西——go figure),我认为静态调暗var会有所帮助。

为什么这个标签是[javascript]?@Jeeped Hhmmm,由于VBA中的
getElementById
,这称为DOM或文档对象模型。看见虽然微软在这方面有了新的(呃)尝试,但微软和Javascript的观点通常并不一致,这一点在这里是肯定的。我有几个问题。鉴于我的代码位于S上,我必须用工作表(“表1”)书写