2017-12-18 372 views
0

我對如何從這個網頁刮數據的問題:VBA動態網頁湊的Excel

http://tvc4.forexpros.com/init.php?family_prefix=tvc4&carrier=64694b96ed4909e815f1d10605ae4e83&time=1513525898&domain_ID=70&lang_ID=70&timezone_ID=31&pair_ID=171&interval=86400&refresh=4&session=session&client=1&user=200743128&width=650&height=750&init_page=instrument&m_pids=&watchlist=&site=https://au.investing.com&version=1.11.2

它出現在IFRAME舉行和一堆JavaScript編程的出現在屏幕上。

當我嘗試收集在iframe下存放的span或div或tr標籤中的元素時,我似乎無法收集其中的數據。

我的目標是在class =「pane-legend-item-value pane-legend-line main」的元素內部保存的innertext。

顯然光標所在的那個特定的時間在屏幕上的innerText會因改變。所以我試圖做的是設置一個IE瀏覽器,它已經加載了頁面,光標放在正確的位置,在圖表的最後(爲了給我最後一個數據點),然後你可以將光標移出屏幕,然後我寫了一些簡單的代碼來抓取該IE窗口,然後嘗試了GetElements,但此時我無法獲取任何數據。

這是到目前爲止我的代碼,它非常粗略的,因爲我一直在試圖編輯,因爲我讀更多的選擇,但還沒有任何勝:(...任何想法或幫助將不勝感激!(截屏也是在底部)

Sub InvestingCom() 

    Dim IE As InternetExplorer 
    Dim htmldoc As MSHTML.IHTMLDocument 'Document object 
    Dim eleColth As MSHTML.IHTMLElementCollection 'Element collection for th tags 
    Dim eleColtr As MSHTML.IHTMLElementCollection 'Element collection for tr tags 
    Dim eleColtd As MSHTML.IHTMLElementCollection 'Element collection for td tags 
    Dim eleRow As MSHTML.IHTMLElement 'Row elements 
    Dim eleCol As MSHTML.IHTMLElement 'Column elements 
    Dim elehr As MSHTML.IHTMLElement 'Header Element 
    Dim iframeDoc As MSHTML.HTMLDocument 
    Dim frame As HTMLIFrame 
    Dim ieURL As String 'URL 

    'Take Control of Open IE 
    marker = 0 
    Set objShell = CreateObject("Shell.Application") 
    IE_count = objShell.Windows.Count 
    For x = 0 To (IE_count - 1) 
     On Error Resume Next 
     my_url = objShell.Windows(x).document.Location 
     my_title = objShell.Windows(x).document.Title 

     If my_title Like "*" & "*" Then 'compare to find if the desired web page is already open 
      Set IE = objShell.Windows(x) 
      marker = 1 
      Exit For 
     Else 
     End If 
    Next 

    'Extract data 
    Set htmldoc = IE.document 'Document webpage 

    ' I have tried span, tr, td etc tags and various other options 
    ' I have never actually tried collecting an HTMLFrame but googled it however was unsuccessful 
End Sub 

Screenshot of the already existing IE which excel can find and talk to with excel and VB open on the other screen and the data I would like to scrape

回答

1

這是真的,我很難從頁面處理兩個嵌套iframes來收集所需的內容。但不管怎麼說,我終於固定它。運行下面的代碼,並獲得您所要求的內容:

Sub forexpros() 
    Dim IE As New InternetExplorer, html As HTMLDocument 
    Dim frm As Object, frmano As Object, post As Object 

    With IE 
     .Visible = True 
     .navigate "http://tvc4.forexpros.com/init.php?family_prefix=tvc4&carrier=64694b96ed4909e815f1d10605ae4e83&time=1513525898&domain_ID=70&lang_ID=70&timezone_ID=31&pair_ID=171&interval=86400&refresh=4&session=session&client=1&user=200743128&width=650&height=750&init_page=instrument&m_pids=&watchlist=&site=https://au.investing.com&version=1.11.2" 
     Do Until .readyState = READYSTATE_COMPLETE: Loop 
     Application.Wait (Now + TimeValue("0:00:05")) 
     Set frm = .document.getElementsByClassName("abs") ''this is the first iframe 
     .navigate frm(0).src 
     Do Until .readyState = READYSTATE_COMPLETE: Loop 
     Application.Wait (Now + TimeValue("0:00:05")) 
     Set html = .document 
    End With 

    Set frmano = html.getElementsByTagName("iframe")(0).contentWindow.document ''this is the second iframe 

    For Each post In frmano.getElementsByClassName("pane-legend-item-value pane-legend-line main") 
     Debug.Print post.innerText 
    Next post 
    IE.Quit 
End Sub