Retrieving specific data from website through excel

筅森魡賤 提交于 2019-12-13 04:59:22

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/23206774/retrieving-specific-data-from-website-through-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!