Excel VBA:迭代代码,但需要DIV类的第二或第三实例

我有下面的代码,从我以前提到的Tim Williams的问题开始。 然而,在工作几分钟后,我想出了一些其他的东西,这是一个新的要求,并没有提出原来的问题。 给定下面的代码和下面的HTML结构如何修改代码来从第二个甚至第三个DIV使用相同的“右头”类提取数据? 孩子DIV没有课程或ID,他们只是包装。

这里是HTML:

<div class="right-header"> <div>Entry 1</div> <div>Entry 2</div> <div>Entry 3</div> <div>Entry 4</div> <div>Entry 5</div> <div>Entry 6</div> </div> <div class="right-header"> <div>Entry 1</div> <div>Entry 2</div> <div>Entry 3</div> <div>Entry 4</div> <div>Entry 5</div> <div>Entry 6</div> </div> <div class="right-header"> <div>Entry 1</div> <div>Entry 2</div> <div>Entry 3</div> <div>Entry 4</div> <div>Entry 5</div> <div>Entry 6</div> </div> 

这是Tim Williams修改后的VBA:

 Sub extract() Dim IE As InternetExplorer Dim topDiv, div, childDivs, tc As String, cntr Set IE = New InternetExplorerMedium IE.Visible = False IE.Navigate2 "C:\Users\john\Documents\Test.html" ' Wait while IE loading Do While IE.Busy Application.Wait DateAdd("s", 1, Now) Loop Set topDiv = IE.document.getElementsByClassName("right-header")(0) Set childDivs = topDiv.getElementsByTagName("div") cntr = 2 For Each div In childDivs tc = Trim(div.textContent) If tc <> "" Then Select Case Right(tc, 1) Case "<div>" 'not sure whether you should be seeing HTML in textcontent...? Range("B" & cntr) = CStr(tc) Case "%" Range("C" & cntr).Value = tc cntr = cntr + 1 Case 0 Range("C" & cntr).Value = tc Case Else Range("A" & cntr).Value = tc End Select End If cntr = cntr + 1 Next div Sheets("Sheet3").Range("A1").Value = topDiv.textContent 'Cleanup IE.Quit Set IE = Nothing End Sub 

谢谢大家,对于原来那么接近的附加问题感到抱歉。

如果div的数量已知,则可以将其放在一个循环中

  Sub extract() Dim IE As InternetExplorer Dim topDiv, div, childDivs, tc As String, cntr Set IE = New InternetExplorerMedium IE.Visible = False IE.Navigate2 "C:\Nitesh\test.html" ' Wait while IE loading Do While IE.Busy Application.Wait DateAdd("s", 1, Now) Loop For i = 0 To 2 Set topDiv = IE.document.getElementsByClassName("right-header")(i) Set childDivs = topDiv.getElementsByTagName("div") cntr = 2 For Each div In childDivs tc = Trim(div.textContent) If tc <> "" Then Select Case Right(tc, 1) Case "<div>" 'not sure whether you should be seeing HTML in textcontent...? Range("B" & cntr).Offset(0, i) = CStr(tc) Case "%" Range("C" & cntr).Offset(0, i).Value = tc cntr = cntr + 1 Case 0 Range("C" & cntr).Offset(0, i).Value = tc Case Else Range("A" & cntr).Offset(0, i).Value = tc End Select End If cntr = cntr + 1 Next div Next i End Sub 

并通过i来抵消你所有的输出结果在一个新的列。

得到Divs

 Sub extract() Dim IE As InternetExplorer Dim topDivs, topDiv, div, childDivs, tc As String, cntr Set IE = New InternetExplorerMedium IE.Visible = False IE.Navigate2 "C:\Users\john\Documents\Test.html" ' Wait while IE loading Do While IE.Busy Application.Wait DateAdd("s", 1, Now) Loop cntr = 2 'get all the top-level divs Set topDivs = IE.document.getElementsByClassName("right-header") 'loop over the top-level divs For Each topDiv In topDivs 'get child divs for this top-level div Set childDivs = topDiv.getElementsByTagName("div") For Each div In childDivs tc = Trim(div.textContent) If tc <> "" Then Select Case Right(tc, 1) Case "<div>" 'not sure whether you should be seeing HTML in textcontent...? Range("B" & cntr) = CStr(tc) Case "%" Range("C" & cntr).Value = tc cntr = cntr + 1 Case 0 Range("C" & cntr).Value = tc Case Else Range("A" & cntr).Value = tc End Select End If cntr = cntr + 1 Next div Next topDiv 'Sheets("Sheet3").Range("A1").Value = topDiv.textContent 'Cleanup IE.Quit Set IE = Nothing End Sub