VBA: Get value by Class name

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
771
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
 

Some videos you may like

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Kyle123

Well-known Member
Joined
Jan 24, 2012
Messages
2,708
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
 

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
771

ADVERTISEMENT

Thanks, can you let me know how I would put this into a cell?
 

Kyle123

Well-known Member
Joined
Jan 24, 2012
Messages
2,708
Code:
SomeWorksheet.Range("A1").value2 = html.querySelector(".price-details--wrapper .value").innerText
 

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
771
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
 

Watch MrExcel Video

Forum statistics

Threads
1,119,073
Messages
5,575,980
Members
412,693
Latest member
SJC2020
Top