XMLHTTP获取子元素

XMLHTTP获取子元素

本文介绍了需要帮助使用Excel XMLHTTP获取子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以成功地获得所有的游戏,但是不能再使用每个循环获得游戏细节。我总是得到这个错误

 对象不支持此属性或方法
/ pre>

在这一行上

  Debug.Print game.getElementsByClassName (date)innerText 

以下是我的代码,

  Dim doc As HTMLDocument 

设置doc =新建HTMLDocument
使用CreateObject(MSXML2.XMLHTTP)
。打开GET,http://foo.bar
.send
做:DoEvents:循环直到.readyState = 4
doc.body.innerHTML = .responseText
.abort
结束

设置游戏= doc.getElementsByClassName(match)
对于每个游戏在游戏
Debug.Print game.getElementsByClassName(date ).innerText
下一个游戏


解决方案

a href =https://msdn.microsoft.com/en-us/library/ff975198(v=vs.85).aspx =nofollow> getElementsByClassName方法是一个集合。如果您想参考收藏集中的单个项目,您将必须提供的最小值是零基索引的整数。

  Debug.Print game.getElementsByClassName(date)(0).innerText 

将在游戏元素(这是集合本身的一部分)中显示一个日期类的第一个元素。



  for el = 0 to game.getElementsByClassName(date)。length  -  1 
Debug.Print game.getElementsByClassName(date)(el).innerText
next el

.Length 是一个基于单数的计数,因此您需要减去 1 以匹配基于零的索引。


I can successfully get all the 'games' but can't then get the games details using the for each loop. I always get this error

object doesn't support this property or method

On this line

 Debug.Print game.getElementsByClassName("date").innerText

Below is my code,

Dim doc As HTMLDocument

    Set doc = New HTMLDocument
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "http://foo.bar"
        .send
        Do: DoEvents: Loop Until .readyState = 4
        doc.body.innerHTML = .responseText
        .abort
    End With

    Set games = doc.getElementsByClassName("match")
    For Each game In games
        Debug.Print game.getElementsByClassName("date").innerText
    Next game
解决方案

The getElementsByClassName method is a collection. If you want to refer to an individual item within the collection the minimum that you will have to provide is an integer for the zero-based index.

Debug.Print game.getElementsByClassName("date")(0).innerText

That will show the first element with a class of date within the game element (which is part of a collection itself).

Alternately, cycle through them.

for el = 0 to game.getElementsByClassName("date").length - 1
    Debug.Print game.getElementsByClassName("date")(el).innerText
next el

The .Length is a one-based count so you need to subtract 1 to match the zero-based index.

这篇关于需要帮助使用Excel XMLHTTP获取子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:43