2017-09-25 8 views
1

絶えず私はHTMLをいじりやウェブサイトから値をプルする方法を学習しようとしているプルしようとすると、担当者の値 - 実行時エラー「を-2147417848

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

Automation error 

The object invoked has disconnected from its clients 

を得ます。このテストでは、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> 
+0

'getelementsbyID'は' getelementbyID'でなければならず、コレクションではなく単一の要素を返します。 IDはドキュメント内で一意である必要があります。 –

+0

@TimWilliamsは絶対に正しいですか、ランダムな質問ですが、あなたのコンピュータの地域は米国以外に設定されていますか? – chiliNUT

+0

@chiliNUT米国に設定されています、なぜですか? – dwirony

答えて

1

は、ここでは、2つの方法です:

Set doc = IE.document 

'1. Drill down level by level 
Set el = doc.getElementById("top-cards") 
Debug.Print el.getElementsByTagName("div")(0). _ 
       getElementsByTagName("div")(0). _ 
       getElementsByTagName("span")(0).innerText 

'2. Use a query selector 
Debug.Print doc.querySelector("#top-cards div div span").innerText 
+0

最初の解決策を試しても、 'Set doc = IE.Document'行に同じ実行時エラーが返されます。 – dwirony

+0

私のために働きます(URLはあなたの質問と同じではありません; https://stackoverflow.com/users/7668613/dwirony?tab = topactivity') –

+0

私はomegastripesが正しいと私は間違っていると言っていますActiveX接続。私はちょっとびっくりしています。エラーメッセージを「オートメーションエラー:インターフェイスが不明です」に変更しました。設定を少しでも混乱させるつもりです。 – dwirony

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 
+0

私はこのコードを試して、タイプの不一致を得ましたが、これは本当に面白いです!私は間違いなくこれを見るつもりです。 – dwirony

関連する問題