Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.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 尝试提取rep值-运行时错误'-2147417848_Html_Excel_Vba - Fatal编程技术网

Html 尝试提取rep值-运行时错误'-2147417848

Html 尝试提取rep值-运行时错误'-2147417848,html,excel,vba,Html,Excel,Vba,不断地 Run-time error '-2147417848 (80010108)': Automation error The object invoked has disconnected from its clients 我在搞HTML,试图学习如何从网站中获取价值。对于这个测试,我打开我的堆栈溢出配置文件,并将rep值放入单元格中。我已经启用了所有的参考库,但是我不断地遇到这个错误 Option Explicit Sub GetTheValue() Dim IE As I

不断地

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

Automation error

The object invoked has disconnected from its clients
我在搞HTML,试图学习如何从网站中获取价值。对于这个测试,我打开我的堆栈溢出配置文件,并将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>

名声
897
这里有两种方法:

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进行刮取:

子测试()
使用CreateObject(“MSXML2.XMLHTTP”)
.打开“获取”https://stackoverflow.com/users/7668613/dwirony”“错
.发送

调试.打印CLng(拆分(拆分(.ResponseText,“”“声誉”“>”,2)(1),"
getelementsbyID
应该是
getelementbyID
并返回单个元素,而不是集合。ID在文档中应该是唯一的。@TimWilliams是绝对正确的,也是一个随机问题,你的计算机的区域是否设置为非US?@chiliNUT它设置为US,为什么?@Tim Williams我对HTML相当陌生-当你说它返回si时ngle元素而不是集合,你是说不需要循环吗?@TimWilliams显然
getelementsbyID
是错误的,但即使引用
IE.Document
并由IE ActiveX断开连接引起,也会出现错误,如图所示。@dIrony尝试使用IE的各种类型和模式。查看。尝试您的第一个解决方案仍然会在
Set doc=IE.Document
行返回相同的运行时错误。这对我来说是有效的(注意URL与您的问题不同;
https://stackoverflow.com/users/7668613/dwirony?tab=topactivity
)嗯,我认为omegastripes说我的ActiveX连接有点问题是对的。我有点搞砸了,我收到了错误消息,要改成
自动错误:接口未知。
我要继续搞乱一些设置。我弄明白了-我必须取消选中“启用保护模式”在我的安全设置中。它现在运行得很好。谢谢。我尝试了这段代码,得到了一个类型不匹配,但这真的很有趣!我一定要看看这个。