2017-06-30 53 views
0

我無法使用span標記並抓取「1日全部返回」的內部文本。刮'span'標記不起作用

這裏是網頁:

http://www.morningstar.com/funds/XNAS/DODFX/quote.html

這裏是我的代碼

Sub Macro1() 
' 

link = "http://www.morningstar.com/funds/XNAS/DODFX/quote.html" 

Set ie = CreateObject("InternetExplorer.Application") 
     With ie 
     .Visible = True 
     .navigate link 
     Do 
     DoEvents 
     Loop Until ie.readyState = READYSTATE_COMPLETE 
     Dim doc As HTMLDocument 
     Set doc = ie.document 
     While ie.readyState <> 4 

     Wend 
     On Error Resume Next 

     Application.Wait (Now + TimeValue("00:00:02")) 

     range("B5").Offset(0, 0).Value = doc.getElementById("msqt_summary")(0).getElementsByClassName("gr_colm_a2b")(0).getElementsByTagName("span")(0).innerText 

     End With 
     ie.Quit 
      Application.EnableEvents = True 

' 
End Sub 
+0

iframe是在頁面中。這是一個問題。 – MacroMarc

+0

這是一個url(包含完全相同的項目),你應該試着用iframe來獲取你的內容:http://quotes.morningstar.com/fund/fundquote/f?t=DODFX&culture=en_us&platform=RET&viewId1= 2313453521&viewId2 = 3167723247&viewId3 = 3474403256&測試= QuoteiFrame – SIM

回答

0

這是你能得到你的數據。數據確實在iframe中。

Sub Web_Data() 

    Dim IE As New InternetExplorer, html As HTMLDocument 
    Dim elem As Object, post As Object 

    With IE 
     .Visible = False 
     .navigate "http://www.morningstar.com/funds/XNAS/DODFX/quote.html" 
     Do While .readyState <> READYSTATE_COMPLETE: Loop 
     Set elem = .document.getElementById("quote_quicktake").getElementsByTagName("iframe")(0) 
     .navigate elem.src 
     While .readyState < 4: DoEvents: Wend 
     Set html = .document 
    End With 
    Set post = html.getElementsByClassName("gr_text_bigprice")(1).getElementsByTagName("span")(1) 
    MsgBox post.innerText 

    IE.Quit 
End Sub 

輸出:

-0.67