2016-03-20 71 views
1

我試圖從一個表格內的網站下載數據。用各種鏈接解析一個帶有VBA的網站

只要我知道直接鏈接到頁面,實際下載工作正常。一頁雖然有不同的子頁面上的數據,我會先加載鏈接,然後按照第1頁上的每個鏈接。

問題是,一旦我加載子頁面,對象變量持有初始鏈接丟失。我怎樣才能保留對象變量?

到目前爲止我的代碼看起來是這樣的:

 ... 
    more code here 
    ... 

    ieApp.Navigate "http://www.website.com/blabla" 
    Do While ieApp.Busy: Sleep 500: DoEvents: Loop 
    Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop 

    Set rcl = ieApp.Document.getElementsbyClassName("col-md-3") 

    For Each ahref In rcl(0).getElementsByTagName("a") 
     ieApp.Navigate ahref.href 
     Do While ieApp.Busy: Sleep 500: DoEvents: Loop 
     Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop 

     ' Now get the data 

     Call subSaveRecords 

    Next 

    ... 
    continue more 
    ... 

後基本上我開始內ieApp.navigate的對於每個RCL-對象/ AHREF目標丟失的ieApp有一個新的頁面。如何在保留對象值的同時繼續前進到新頁面?

感謝您的幫助。

回答

1

您不能簡單地創建一個新的對象來打開子鏈接並將該對象傳遞給子例程以獲取數據嗎?

set ie2 = CreateObject(internetexplorer.application) 
ieApp.Navigate "http://www.website.com/blabla" 
Do While ieApp.Busy: Sleep 500: DoEvents: Loop 
Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop 

Set rcl = ieApp.Document.getElementsbyClassName("col-md-3") 

For Each ahref In rcl(0).getElementsByTagName("a") 
    ie2.Navigate ahref.href 
    Do While ie2.Busy: Sleep 500: DoEvents: Loop 
    Do Until ie2.readyState = READYSTATE_COMPLETE: DoEvents: Loop 

    'Now get the data 

    Call subSaveRecords ie2 
Next 

或者您可以在更改頁面之前將鏈接添加到集合或數組。

ieApp.Navigate "http://www.website.com/blabla" 
Do While ieApp.Busy: Sleep 500: DoEvents: Loop 
Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop 

Set rcl = ieApp.Document.getElementsbyClassName("col-md-3") 
For Each ahref in rcl(0).getElementsByTagName("a") 
    linkCollection.add ahref.href 
Next ahref 

For Each ahref In linkCollection 
    ieApp.Navigate ahref 
    Do While ieApp.Busy: Sleep 500: DoEvents: Loop 
    Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop 

    'Now get the data 

    Call subSaveRecords 
Next 
+0

太棒了,非常感謝。我去了第二個選擇,因爲該網站是我必須登錄的網站,我覺得打開兩個瀏覽器不太優雅。很好的幫助! – Matth