试图拉回rep值 - 运行时错误' -2147417848

时间:2017-09-25 17:10:33

标签: html excel excel-vba vba

不断获得

Run-time error '-2147417848 (80010108)':

Automation error

The object invoked has disconnected from its clients

我正在搞乱HTML并尝试学习如何从网站中提取价值。对于此测试,我打开Stack Overflow配置文件并将我的rep值拉到单元格中。我启用了所有参考库,但我不断收到此错误。

Option Explicit
Sub GetTheValue()
    Dim IE As InternetExplorer, retrievedvalue As Variant, oHTML_Element As IHTMLElement

    Set IE = New InternetExplorerMedium
    IE.Visible = True

    IE.navigate "https://stackoverflow.com/users/7668613/dwirony"

    Application.Wait (Now + TimeValue("0:00:05"))

    For Each oHTML_Element In IE.Document.getelementsbyID("top-cards")
        If oHTML_Element.classname = "g-col fl-none -rep" Then
            retrievedvalue = oHTML_Element.InnerText
            Exit For
        End If
    Next oHTML_Element

    Workbooks("Book1").Worksheets("Sheet1").Range("A1").Value = retrievedvalue
End Sub

行上发生错误

For Each oHTML_Element In IE.Document.getelementsbyID("top-cards")

这是我尝试阅读的片段:

<div id="top-cards" class="g-row _gutters p-highlights">

<aside class="g-col g-column -card -reputation js-highlight-box-reputation">
    <h1 class="g-col -title">Reputation</h1>
    <div class="g-row -row-first">
        <div class="g-col g-column">
            <div class="g-row _gutters fl-none">
                <span class="g-col fl-none -rep">897</span>

2 个答案:

答案 0 :(得分:1)

以下两种方法:

 var timer = new System.Threading.Timer(async (ev) => {
     try{
          using (StreamWriter sw = File.AppendText(path + @"\log_test.txt")) {
                sw.WriteLineAsync("-------- LOG BEGIN --------" + Environment.NewLine);
                 try {
                     bool sync = await AzureSync.Begin();
                 }
                 catch (Exception ex) {
                     sw.WriteLine(DateTime.Now + "**** ERROR ****" + Environment.NewLine);
                     sw.WriteLine(ex.ToString() + Environment.NewLine);
                 }
             }
      }catch{}; //swallow exception and prevent it from taking down the process/IIS app pool
 }, null, TimeSpan.Zero, 120000);   //120000 milliseconds is 2 Minutes

答案 1 :(得分:1)

尝试使用XHR:

Sub Test()

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://stackoverflow.com/users/7668613/dwirony", False
        .Send
        Debug.Print CLng(Split(Split(.ResponseText, """reputation"">", 2)(1), "<", 2)(0))
    End With

End Sub
相关问题