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

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
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,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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