0

i am trying to get data using MSXML2.XMLHTTP but it didn't work

any ideas?

Sub getdata

Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim website As String
Dim price As String
Dim sht As Worksheet


Application.DisplayAlerts = False
Set sht = ActiveSheet


    On Error Resume Next
    website = "https://shopee.co.id/AFI-EC-Tshirt-Yumia-(LD-90-P-57)-i.10221730.5568491283"
    Set request = CreateObject("MSXML2.XMLHTTP")

   request.Open "GET", website, False
   request.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
   request.send
   response = StrConv(request.responseBody, vbUnicode)
   html.DocumentElement.innerHTML = response

    
    price = html.querySelector("div.AJyN7v")(0).innerText
    
    Debug.Print price
    
    Application.StatusBar = ""
    On Error GoTo 0
Application.DisplayAlerts = True``
End Sub

I have done many ways but still not working , hope someone can help me

5
  • 3
    "didn't work" is not a useful description of what happens when you run your code. Do you get an error? If Yes what error and which line. Please review How to Ask and consider improving your post with some additional details. Commented Feb 25, 2021 at 22:56
  • no error message Commented Feb 25, 2021 at 23:41
  • can you try running my code? I want to get the Price Commented Feb 25, 2021 at 23:42
  • Try without On Error Resume Next Commented Feb 25, 2021 at 23:50
  • there is an error message Object variable or With block variable not set Commented Feb 26, 2021 at 0:03

1 Answer 1

1

Pretty much everything on that page requires javascript to load. Javascript doesn't run with xmlhttp request to landing page so price never gets retrieved..

The price is being retrieved dynamically from an additional API call returning json.

If you examine the url you will have the following:

https://shopee.co.id/AFI-EC-Tshirt-Yumia-(LD-90-P-57)-i.10221730.5568491283

The last set of consecutive numbers is the product id i.e. 5568491283.

If you open the network tab of dev tools F12, and press F5 to refresh the web traffic that updates the page, then check on the xhr only traffic, then input your product id into the search box, the first result retrieved is the xhr which is returning the price:

https://shopee.co.id/api/v2/item/get?itemid=5568491283&shopid=10221730

The response is json so you will need a json parser to extract the result (or use regex on string - less preferable)

enter image description here

In the headers sub-tab you can view info about the xhr request made.

Check the terms and conditions to see if scraping allowed and also whether there is an public API for retrieving this data.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this elaborative visual. It will immensely help future readers to understand what really goes on in the background.
how to parser json ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.