1
<span itemprop="streetAddress">

    **94 Grand St**

</span>

how to get this data through getelementby method in excel vba

I have tried getelementbyid, getelementbyname etc. but nothing is working

Option Explicit

Sub find()
'Uses late binding, or add reference to Microsoft HTML Object Library
'  and change variable Types to use intellisense
Dim ie As Object 'InternetExplorer.Application
Dim html As Object 'HTMLDocument
Dim Listings As Object 'IHTMLElementCollection
Dim l As Object 'IHTMLElement
Dim r As Long
    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = False
        .Navigate "http://www.yelp.com/biz/if-boutique-new-york#query:boutique"
        ' Don't show window
        'Wait until IE is done loading page
        Do While .readyState <> 4
            Application.StatusBar = "Downloading information, Please wait..."
            DoEvents
        Loop
        Set html = .Document
    End With
    Set Listings = html.getElementsByTagName("span") ' ## returns the list
    MsgBox (Listings(0))
    For Each l In Listings
        '## make sure this list item looks like the listings Div Class:
        '   then, build the string to put in your cell
        Range("A1").Offset(r, 0).Value = l.innerText
            r = r + 1
    Next

Set html = Nothing
Set ie = Nothing
End Sub

The above program is used by me to get the innerText value inside the span tag... but its not working

  • 1
    Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: [Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist) – Doug Glancy Oct 12 '13 at 14:56
  • 1
    What does it do instead of working? Your getElementsByTagName look ok, so what happens when you run this? – Tim Williams Oct 12 '13 at 15:59
  • Have u debugged ur code .. where u r getting error ? – Sathish Kothandam Oct 12 '13 at 16:09
  • 2
    Did you accept the answer on the previous question which is nearly identical? I notice you did not use the proper `If/Then` logic to check the `l.innerText`, as I previously suggested. Without looking at the specifics of this question, since it seems so similar to your previous one, BrettDJ's answer below looks good, but as a matter of etiquette, since you've used nearly *all* the code I provided you earlier, an upvote or acceptance of that answer would be appreciated. – David Zemens Oct 13 '13 at 18:28

1 Answers1

1

For the single result you are looking for in detail you want to use these two lines in your code (there is only 1 listing at the detailed level)

Adapt your IE code

  Set Listings = html.getElementbyid("bizInfoBody") ' ## returns the list
  Range("A1").Offset(r, 0).Value = Listings.innerText

with XMLHTTP

Sub GetTxt()
Dim objXmlHTTP As Object
Dim objHtmlDoc As Object
Dim objHtmlBody As Object
Dim objTbl As Object

Dim strResponse As String
Dim strSite As String


Set objHtmlDoc = CreateObject("htmlfile")
Set objHtmlBody = objHtmlDoc.body

Set objXmlHTTP = CreateObject("MSXML2.XMLHTTP")
strSite = "http://www.yelp.com/biz/if-boutique-new-york"

With objXmlHTTP
    .Open "GET", strSite, False
    .Send
    If .Status = 200 Then
    strResponse = .responseText
    objHtmlBody.innerHTML = objXmlHTTP.responseText
    Set objTbl = objHtmlBody.Document.getElementbyid("bizInfoBody")
    MsgBox objTbl.innerText
    End If
End With

End Sub
brettdj
  • 54,857
  • 16
  • 114
  • 177