VBA:在表上进行Web爬网(<table>中<tr>标记下的<td>标记

时间:2019-11-28 08:26:32

标签: html

我正在尝试使用VBA从html表中获取数据。表格在网页的第三个标签中的位置。我能够获取数据,但发现了一些错误消息。 问题是输入代码,这里的代码不会循环,如果表中的数据为空,那么它将退出sub。我需要它再次循环。 我需要所有的表数据都是td的。

Sub WEBBSCRAP()

        Dim ieObj As InternetExplorer
        Dim htmlEle As IHTMLElement
        Dim Lr1 As Long, Lr2 As Long, Lp As Long
        Dim StartTime As Double
        Dim MinutesElapsed As String

On Error GoTo err:

        Lr1 = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
        Lr2 = ThisWorkbook.Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row + 1

        StartTime = Timer

check:
     For Lp = 2 To Lr1

Set ieObj = New InternetExplorer  
ieObj.Visible = True
ieObj.navigate "https://www.URL.com/sc/wo/Workorders/index?id=" & ThisWorkbook.Sheets("Sheet1").Range("A" & Lp).Value

Application.Wait Now + TimeValue("00:00:07")  ' give the webpage some time to load all content

For Each htmlEle In ieObj.document.getElementsByClassName("table-basic labor-performed-table")(0).getElementsByTagName("tr")
If htmlEle.Children(0).textContent = "Total Hours" Then Exit For
 With ThisWorkbook.Sheets("Sheet2")
    .Range("A" & Lr2).Value = htmlEle.Children(0).textContent
    .Range("B" & Lr2).Value = htmlEle.Children(1).textContent
    .Range("C" & Lr2).Value = htmlEle.Children(2).textContent
    .Range("D" & Lr2).Value = htmlEle.Children(3).textContent
    .Range("E" & Lr2).Value = htmlEle.Children(4).textContent
    .Range("F" & Lr2).Value = htmlEle.Children(5).textContent
    .Range("G" & Lr2).Value = ThisWorkbook.Sheets("Sheet1").Range("A" & Lp).Value
   Lr2 = Lr2 + 1
   End With
Next htmlEle
ieObj.Quit

If Lp = Lr1 Then
        MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
        MsgBox "This code ran successfully in " & MinutesElapsed & " minutes", vbInformation
        Exit Sub
    End If

Next Lp
err:
GoTo check

End Sub


`

<table class="table-basic labor-performed-table">
  
<thead class="table-head-basic">
<tr>
	<th>Name</th>
	<th>Work Date</th>
	<th>Time In</th>
	<th>Time Out</th>
	<th class="text-right"># of Techs</th>

	<th class="text-right">Reg. Hrs</th>
                                        
</tr>
                                   
</thead>
                                   
<tbody>
                                        
<tr class="table-row">
                                            
	<td data-bind="text: userName">DESIGN4</td>
                                            
	<td data-bind="text: workDate">Dec 20 2018</td>
                                            
	<td data-bind="text: checkInTime">12:09 CST  </td>
                                            
	<td data-bind="text: checkOutTime">13:42 CST  </td>
                                            
	<td class="text-right" data-bind="text: numberOfTechs">1</td>
                                           
	<td class="text-right" data-bind="text: hoursStr">1.55</td>
                                        
</tr>
                                        
                                        
<tr class="table-row">
                                            
	<td data-bind="text: userName">DESIGN4</td>
                                            
	<td data-bind="text: workDate">Feb 25 2019</td>
                                            
	<td data-bind="text: checkInTime">14:48 CST  </td>
                                           
	<td data-bind="text: checkOutTime">14:49 CST  </td>
                                            
	<td class="text-right" data-bind="text: numberOfTechs">1</td>
                                            
	<td class="text-right" data-bind="text: hoursStr">0.02</td>
                                        
</tr>
                                        
<!-- /ko -->
                                        
<tr class="table-row" data-bind="visible : $root.laborItems().length > 0">
                                            
	<td class="text-right" colspan="5"><strong>Total Hours</strong></td>
                                           
	<td class="text-right" data-bind="text: laborTotalHours">1.57</td>
                  
</tr>

                                        
	<tr class="table-row" style="display: none;" data-bind="visible : $root.laborItems().length == 0">
                                            
	<td colspan="7">No Labor Performed</td>
                                       
</tr>
                                    
</tbody>
                                
</table>
</div>

0 个答案:

没有答案