excel - 使用“分页”更改网站页面的宏
问题描述
我正在尝试在 Excel 中编写一个宏来更改以下网站中的页面:https ://cebra.com.ar/category/18/Disfraces-and-Accesorios.html
该页面没有可单击的典型按钮,使用该按钮,此页面上存在的一些解决方案不起作用......
下面是我编写的代码,它运行良好,只是缺少最终点击。
有人可以帮我开发这个宏吗?非常感谢您!
Sub change_webpage()
Dim ie As InternetExplorer
Dim lis As IHTMLElementCollection
Dim nextLi As HTMLLIElement, i As Long
sheetnom = ActiveSheet.Name
link = "https://cebra.com.ar/category/18/Disfraces-y-Accesorios.html"
Worksheets(sheetnom).Activate
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate link
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Loading Web page …"
DoEvents
Loop
newNum = -1
Set objIE = New InternetExplorer
objIE.navigate fullUrl
Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop
Set currPage = ie.document
Do Until oldNum = newNum
oldNum = newNum
newNum = currPage.getElementsByClassName("box-data").length
Application.Wait Now + TimeSerial(0, 0, 2)
currPage.parentWindow.scrollBy 0, 100000
Application.Wait Now + TimeSerial(0, 0, 2)
If newNum > 400 Then newNum = 400
Loop
Set lis = ie.document.getElementsByClassName("pagination")
Set nextLi = Nothing
i = 0
While i < lis.length And nextLi Is Nothing
If lis(i).innerText = "2" Then Set nextLi = lis(i)
i = i + 1
Wend
If Not nextLi Is Nothing Then
nextLi.Change = Active
End If
'ie.Quit
link = Empty
Sheets(sheetnom).Select
Range("A1").Select
MsgBox "Done"
End Sub
解决方案
该页面对我来说无法正确加载,但是在您找到页面数并单击>按钮直到用尽所有页面之前,类似以下内容的内容应该可以工作。
Option Explicit
'VBE > Tools > References: Microsoft Internet Controls
Public Sub ClickThroughPages()
Dim ie As New InternetExplorer, numberOfPages As Long, page As Long
With ie
.Visible = True
.Navigate2 "https://cebra.com.ar/category/18/Disfraces-y-Accesorios.html"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
numberOfPages = .querySelectorAll(".setPage").length
'do something with page 1
If numberOfPages > 1 Then
For page = 2 To numberOfPages
.querySelector(".fa-angle-right").Click
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
'do something with next pages
Next
End If
End With
Stop
.Quit
End With
推荐阅读
- java - 如何从本机视图获取属性
- docker - 保存后如何使本地更改反映在 docker 容器中
- postgresql - 如何对接我的 dotnet core + postgresql 应用程序?
- alloy-ui - AlloYUI:带有 showNextYear 和 showPreviousYear 按钮的 aui 日期选择器?
- python - 使用 for 循环在字典中创建内部键
- ubuntu - 如何使用 Ubuntu 18.04 降级到以前版本的 Mono
- delphi - 如何在 Delphi 10.3 中启用源文件删除
- javascript - 无法将复杂对象数组正确转换为 CSV
- javascript - 用钩子反应上下文防止重新渲染
- angular - 如何实现变更检测以使给定的示例有效