2013-10-12 167 views
1
<span itemprop="streetAddress"> 

    **94 Grand St** 

</span> 

如何讓在Excel VBA中從網站獲取的數據

我已經試過的getElementById,getelementbyname等,但沒有通過getelementby方法這個數據正在

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 

上述程序所使用我得到span標記內的innerText值...但它不起作用

+1

問題要求代碼必須表現出對問題的理解最小正在解決。包括嘗試解決方案,爲什麼他們沒有工作,以及預期的結果。另請參閱:[堆棧溢出問題清單](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist) –

+1

它做什麼而不是工作?你的getElementsByTagName看起來不錯,那麼當你運行這個時會發生什麼? –

+0

你有沒有調試過我們的代碼..哪裏出錯? –

回答

1

對於您正在尋找的單個結果詳細信息,您希望在代碼中使用這兩行(僅限於在詳細的級別1名上市)

適應你的IE代碼

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

使用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