Search code examples
htmlvbaexcelyelp

Retrieving specific data from website through excel


I am trying to do something very similar to the below existing example: reference problem

With one small exception, I need to pull only the rating and # of reviews for this listing into 2 separate cells in Excel.

How would I do this in a way without pulling the entire site's data? It seems I need to call a specific html tag or use a command to do this, but I don't know what it is.

Please Help!


Solution

  • This code will retrieve the two pieces of information you requested and place them on the activesheet

    Sub test()
        my_url = "http://www.yelp.com/biz/if-boutique-new-york"
        Set html_doc = CreateObject("htmlfile")
        Set xml_obj = CreateObject("MSXML2.XMLHTTP")
    
        xml_obj.Open "GET", my_url, False
        xml_obj.send
        html_doc.body.innerhtml = xml_obj.responseText
        Set xml_obj = Nothing
    
        Set Results = html_doc.body.getElementsByTagName("i")
        For Each itm In Results
            If InStr(1, itm.outerhtml, "star-img", vbTextCompare) > 0 Then
                numb_stars = itm.getAttribute("title")
                Exit For
            Else
            End If
        Next
    
        Set Results = html_doc.body.getElementsByTagName("span")
        For Each itm In Results
            If InStr(1, itm.outerhtml, "reviewCount", vbTextCompare) > 0 Then
                numb_rev = itm.innertext
                Exit For
            Else
            End If
        Next
    
        ActiveCell = numb_stars
        ActiveCell.Offset(1, 0) = numb_rev
    End Sub