VBA: Get value by Class name

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
840
Hello, trying to get the price from the website:

https://www.tesco.com/groceries/en-GB/products/303105747

So would return £3.00

Any ideas, this is my attempt?

Thanks in advance!

Code:
Sub WebData()
    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim source As Object


    With http
        .Open "GET", "https://www.tesco.com/groceries/en-GB/products/303105747", False
        .send
        html.body.innerHTML = .responseText
    End With
    For Each source In html.getElementsByClassName("data-auto")
        x = x + 1: Cells(x, 1) = source.getAttribute("value")


    Next source
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Like this:
Code:
    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim source As Object


    With http
        .Open "GET", "https://www.tesco.com/groceries/en-GB/products/303105747", False
        .send
        html.body.innerHTML = .responseText
    End With

    MsgBox html.querySelector(".price-details--wrapper .value").innerText
 
Upvote 0
Code:
SomeWorksheet.Range("A1").value2 = html.querySelector(".price-details--wrapper .value").innerText
 
Upvote 0
That is great thanks and works.

I have 1 further question:

How would I loop through URLs, I am thinking

For x = LBound(urls) To UBound(urls)

Sheets("Sheet1").Cells(Sheets("Sheet1").Rows.Count, 1).End(xlUp).Offset(1).Resize(UBound(dogLinks), 17).Value2 = dogLinks
Next x
.Open "GET", url, False
.send
html.body.innerHTML = .responseText
End With
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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