我写了这个子程序,它递归地解析HTML页面树。 我的问题是,当我到达树的底部并且子级退出到上一级时,所有对象都是空的。 这是完整的代码:
Public Row As Integer
Public objIE As Object
Sub GetBranches()
Set objIE = CreateObject("InternetExplorer.Application") ' create new browser
objIE.Visible = True ' make IE visible for viewing login
Row = 2 ' start at line 2 (first line is headers)
GetBranchesRecursively ("241653")
End Sub
Sub GetBranchesRecursively(BranchID As String)
Dim CurrentBranch As Object
Dim subBranches, subBranch As Object
Dim SubBranchID As String
' navigate IE to Branch ID
objIE.navigate "https://casadasereia.net/vbatests/viewtree" & BranchID & ".html"
' wait for browser
Do While objIE.Busy = True Or objIE.readyState <> 4
DoEvents
Loop
Set CurrentBranch = objIE.document.getElementById(BranchID)
' store name and ID of current Branch
Worksheets("Data").Range("A" + CStr(Row)) = Trim(CurrentBranch.innerText) ' Branch name is in the HTML A tag
Worksheets("Data").Range("B" + CStr(Row)) = BranchID
Row = Row + 1
' list of subBranches is the list of LI elements of the UL element adjacent to the A tag
Set subBranches = CurrentBranch.NextSibling.NextSibling.getElementsByTagName("li")
For Each subBranch In subBranches
SubBranchID = subBranch.getElementsByTagName("A")(0).ID ' BranchID is in the id property of the HTML A tag
If InStr(subBranch.className, "node") <> 0 Then
' This is a node, store data and move to next in the list
Worksheets("Data").Range("A" + CStr(Row)) = Trim(subBranch.innerText) ' name is in the HTML A tag
Worksheets("Data").Range("B" + CStr(Row)) = SubBranchID
Row = Row + 1
Else
' This is a branch, move down to the branch
GetBranchesRecursively (SubBranchID)
End If
Next
End Sub
在最后一级的递归(分支ID 99816)中,有4个节点且没有分支。它们的数据已正确存储,并且在For循环结束时,控制权被传递回上一级,但是在这种情况下,所有对象均为空。这是调试器向我显示的内容:
任何人都知道为什么在递归调用之前正确填充这些变量后为何显示为空吗?