VBA Xml Loop

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
840
Hello following code works fine for 1 URL, I am trying to adapt so it runs for all the numerical pages it can find from the baseURL, so for instance the 1 up to the max until there is an error, is this possible?

Thanks.

baseUrl = "https://www.tesco.com/groceries/en-GB/shop/fresh-food/all?include-children=true&page=1"

Code:
Public Sub Data_Pull_Products_and_Prices()


Dim http As Object, html As New HTMLDocument, topics As Object, topics2 As Object, titleElem As Object, topic As HTMLHtmlElement
Dim i As Integer
Dim j As Integer


Set http = CreateObject("MSXML2.XMLHTTP")


baseUrl = "https://www.tesco.com/groceries/en-GB/shop/fresh-food/all?include-children=true&page=1"


URL = URL = baseURL &
http.Open "GET", "https://www.tesco.com/groceries/en-GB/shop/fresh-food/all?include-children=true&page=1", False
http.send
html.body.innerHTML = http.responseText


Set topics = html.getElementsByClassName("product-details--wrapper")
Set topics2 = html.getElementsByClassName("price-control-wrapper")


i = 1


For Each topic In topics
Set titleElem = topic.getElementsByTagName("div")(0)
Sheets(1).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText


i = i + 1


Next


j = 1


For Each topic In topics2
Set titleElem = topic.getElementsByTagName("div")(0)
Sheets(1).Cells(j, 2).Value = titleElem.getElementsByTagName("span")(0).innerText


j = j + 1


Next


End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Got the loop working - now how do I prevent it from pasting over the top of the previous data?

Thanks.

Code:
Public Sub Data_Pull_Products_and_Prices()


Dim http As Object, html As New HTMLDocument, topics As Object, topics2 As Object, titleElem As Object, topic As HTMLHtmlElement
Dim i As Integer
Dim j As Integer
Dim rngURL As Range


Set http = CreateObject("MSXML2.XMLHTTP")


For Each rngURL In Worksheets("Sheet1").Range("E1", Worksheets("Sheet1").Range("E" & Rows.Count).End(xlUp))


http.Open "GET", rngURL, False
http.send
html.body.innerHTML = http.responseText


Set topics = html.getElementsByClassName("product-details--wrapper")
Set topics2 = html.getElementsByClassName("price-control-wrapper")


i = 1


For Each topic In topics
Set titleElem = topic.getElementsByTagName("div")(0)
Sheets(1).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText


i = i + 1


Next


j = 1


For Each topic In topics2
Set titleElem = topic.getElementsByTagName("div")(0)
Sheets(1).Cells(j, 2).Value = titleElem.getElementsByTagName("span")(0).innerText


j = j + 1


Next


Next


End Sub
 
Upvote 0
Many to solve it.

Code:
Public Sub Data_Pull_Products_and_Prices_2()


Dim http As Object, html As New HTMLDocument, topics As Object, topics2 As Object, titleElem As Object, topic As HTMLHtmlElement
Dim i As Integer
Dim j As Integer
Dim rngURL As Range
Dim LastRow As Long
Dim LastRow2 As Long


Application.ScreenUpdating = False


Set http = CreateObject("MSXML2.XMLHTTP")


For Each rngURL In Worksheets("Sheet1").Range("E1", Worksheets("Sheet1").Range("E" & Rows.Count).End(xlUp))


http.Open "GET", rngURL, False
http.send
html.body.innerHTML = http.responseText


DoEvents


Set topics = html.getElementsByClassName("product-details--wrapper")
Set topics2 = html.getElementsByClassName("price-control-wrapper")


i = 1


For Each topic In topics
Set titleElem = topic.getElementsByTagName("div")(0)
Sheets(1).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
i = 1 + LastRow


Next


j = 1


For Each topic In topics2
Set titleElem = topic.getElementsByTagName("div")(0)
Sheets(1).Cells(j, 2).Value = titleElem.getElementsByTagName("span")(0).innerText
LastRow2 = Cells(Rows.Count, "B").End(xlUp).Row
j = 1 + LastRow2


Next


Next


Application.ScreenUpdating = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top