Sharid
Well-known Member
- Joined
- Apr 22, 2007
- Messages
- 1,066
- Office Version
- 2016
- Platform
- Windows
I'm trying to pull of some urls for products of ebay, about 5 pages worth. but I cant get the code to work
In sheet2 A1 you place the keyword like "Laptops" and then IE opens goes to ebay searches Laptops and the urls are to be pulled off then it clicks on the next page tab. However I cant get it to work keeps giving me an error message
In sheet2 A1 you place the keyword like "Laptops" and then IE opens goes to ebay searches Laptops and the urls are to be pulled off then it clicks on the next page tab. However I cant get it to work keeps giving me an error message
VBA Code:
Private Sub CommandButton1_Click()
Dim ie As Object
Dim htmlDoc As Object
Dim nextPageElement As Object
Dim div As Object
Dim link As Object
Dim URL As String
Dim pageNumber As Long
Dim i As Long
' Takes seach from A1 and places it into ebay
URL = "https://www.ebay.co.uk/sch/" & Replace(Worksheets("Sheet2").Range("A1").Value, " ", "+")
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.navigate URL
Do While .Busy Or .readyState <> 4
DoEvents
Loop
End With
Application.Wait Now + TimeSerial(0, 0, 5)
Set htmlDoc = ie.document
pageNumber = 1
i = 2
Do
For Each div In htmlDoc.getElementsByTagName("div")
If div.getAttribute("class") = "vip" Then
Set link = div.getElementsByTagName("a")(0)
Cells(i, 1).Value = link.getAttribute("href")
i = i + 1
End If
Next div
If pageNumber >= 5 Then Exit Do 'the first 5 pages
Set nextPageElement = htmlDoc.getElementById("gspn-next")
If nextPageElement Is Nothing Then Exit Do
' Clicks web next page
nextPageElement.Click 'next web page
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
Application.Wait Now + TimeSerial(0, 0, 5)
Set htmlDoc = ie.document
pageNumber = pageNumber + 1
Loop
ie.Quit
Set ie = Nothing
Set htmlDoc = Nothing
Set nextPageElement = Nothing
Set div = Nothing
Set link = Nothing
MsgBox "All Done"
End Sub