试图拉代表价值 – 运行时错误“-2147417848

不断得到

Run-time error '-2147417848 (80010108)': Automation error The object invoked has disconnected from its clients 

我搞乱了HTML,并试图学习如何从网站拉值。 对于这个testing,我打开我的堆栈溢出configuration文件,并将我的代表值放置在单元格中。 我有我所有的参考库启用,但我不断得到这个错误。

 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> 

这里有两个方法:

 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 

尝试与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