我是在VBA中使用Selenium进行网页抓取的初学者,但是由于网页无法与IE配合使用,因此我不得不更改代码。我的代码已经打开了网页,更改了结果的视图和顺序...,我只需要将产品名称,价格,信息和卖方刮到我的“ RESULTADO”工作表即可。我只能将其用于第一个产品...但是我不能使它适用于结果的每个元素。班级名称或班级名称似乎有问题...但是我确实尝试了所有我能想象的。请帮助...
'CHROME
Const sURL As String = "https://mercadolibre.com.mx"
Dim bot As New ChromeDriver
Dim Catal As Long
Dim FinCatal As Long
Dim VarA As String
Dim myproducts As WebElements
Dim myproduct As WebElement
Dim i As Integer
Dim mysheet As Worksheet
With bot
.get sURL
End With
bot.Window.Maximize
'choose product from catalogue
Sheets("CATALOGO").Select
For Catal = 2 To 1048576
If Cells(Catal, 2) = "" Then
FinCatal = Catal - 1
Catal = 1048576
Else
VarA = Cells(Catal, 3)
'search product in webpage
bot.findElementByName("as_word").SendKeys VarA
bot.FindElementByClass("nav-search-btn").Click
'wait
Do Until InStr(1, bot.Url, "listado") > 0
Application.Wait (DateAdd("s", 1, Now))
DoEvents
Loop
'change view
bot.FindElementByClass("view-options-buttons").Click
'wait
Do Until InStr(1, bot.Url, "DisplayType") > 0
Application.Wait (DateAdd("s", 1, Now))
DoEvents
Loop
'order by lower price
bot.FindElementByClass("ui-dropdown__link").Click
bot.findElementByLinkText("Menor precio").Click
'wait
Do Until InStr(1, bot.Url, "PRICE") > 0
Application.Wait (DateAdd("s", 1, Now))
DoEvents
Loop
'scrape products, price, info and seller
Set mysheet = Sheets("RESULTADO")
i = 1
Set myproducts = bot.FindElementsByClass("results-item highlighted article stack product ")
For Each myproduct In myproducts
mysheet.Cells(i, 1).Value = bot.FindElementByClass("main-title").Text
mysheet.Cells(i, 2).Value = bot.FindElementByClass("item__price").Text
mysheet.Cells(i, 3).Value = bot.FindElementByClass("stack-item-info").Text
mysheet.Cells(i, 4).Value = bot.FindElementByClass("item__brand-title-tos").Text
i = i + 1
Next
End If
Next