我编写了导航到网站的代码,进入内页展开详细信息,然后将 HTML 表复制到新创建的工作表。
导航循环没有问题,它可以正确循环所有元素并正确扩展细节。
复制表会出现问题。第一次复制表时,第二轮只创建新工作表,但不粘贴任何内容。
我 debug.print 变量的内容并且还使用了断点,所以数据在变量中但它没有被粘贴。
如果您需要更多数据,请告诉我。先感谢您!
For Each ele In html.getElementsByTagName("a")
If InStr(1, ele.ID, "cup_cuplv", vbTextCompare) > 0 Then
ele.FireEvent "onclick" ' clicks on an item in the list
PauseTime = 5 ' Set duration in seconds
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop
' For Each ele1 In html.getElementsByClassName("icn_vw icn_tgld")
' If InStr(1, ele1.innerText, "Details", vbTextCompare) > 0 Then
Set ele1 = html.getElementsByClassName("icn_vw icn_tgld")(0)
ele1.FireEvent "onclick" ' clicks to expand details
Set ele1 = Nothing
PauseTime = 10 ' Set duration in seconds
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop
Dim objTable As Object
Dim lRow As Long
Dim lngTable As Long
Dim lngRow As Long
Dim lngCol As Long
Dim ActRw As Long
'Set html = New HTMLDocument
Set NewWS = ThisWorkbook.Sheets.Add(After:=Sheets(Worksheets.Count), Count:=1, Type:=xlWorksheet) 'created new sheet
Set IE = objShell.Windows(IE_count - 1)
Do While IE.Busy: Loop
html.body.innerHTML = IE.Document.body.innerHTML
With html.body
Set objTable = .getElementsByTagName("table")
For lngTable = 0 To objTable.Length - 1
For lngRow = 0 To objTable(lngTable).Rows.Length - 1
For lngCol = 0 To objTable(lngTable).Rows(lngRow).Cells.Length - 1
Worksheets(NewWS).Activate
NewWS.Cells(ActRw + lngRow + 1, lngCol + 1) = objTable(lngTable).Rows(lngRow).Cells(lngCol).innerText ' problem here doesnt paste 2nd 3rd and nth time
Next lngCol
Next lngRow
ActRw = ActRw + objTable(lngTable).Rows.Length + 1
Next lngTable
End With
'Set html.body = Nothing
Set objTable = Nothing
Set NewWS = Nothing
'End If
PauseTime = 3 ' Set duration in seconds
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop
Debug.Print ele1.ID
'Next ele1
Debug.Print ele.ID
html.getElementById("tab_mn2").FireEvent "onclick" ' goes back to main item list
PauseTime = 5 ' Set duration in seconds
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop
End If
Next ele ' goes to next item in main list here