VBA: Get value by Class name

jamescooper

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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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,213,543
Messages
6,114,236
Members
448,555
Latest member
RobertJones1986

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