2016-03-06 38 views
0

我試圖從網頁的一部分中取消數據。爲了進入該部分,我需要填寫一個驗證碼安全代碼並點擊一個按鈕,但這是正確的,因爲安全代碼實際上是寫在頁面的html中的。所以,我創建了一個IE對象,將它驅動到網頁,獲取驗證碼安全代碼,將它寫入適當的框中,點擊提交按鈕,然後獲取html文檔,以便我可以從中取出數據。VBA - 點擊提交按鈕後從網站獲取html的問題

儘管如此,我正按照我提到的順序執行這些步驟,似乎正在獲取的html文檔不是來自頁面的通過驗證碼驗證後的頁面,而是來自驗證碼驗證之前的頁面。

有人會知道我必須做什麼才能獲得正確的html文檔,並且能夠報廢我真正想要的數據嗎?謝謝。

子過程的代碼如下旁邊:

'Getting National fuel prices from ANP 
Sub subANPNationalFuelPrices() 
'Creating variables for the URL and the HTML files 
Dim urlANP As String: urlANP = "http://www.anp.gov.br/preco/prc/Resumo_Semanal_Index.asp" 
Dim htmlANP1 As HTMLDocument 

'Creating the IE object 
Dim IE As InternetExplorer 
Set IE = New InternetExplorer 
IE.Visible = True 

'Making sure that the webpage is fully load 
IE.navigate (urlANP) 
Do While IE.readyState <> READYSTATE_COMPLETE 
Application.StatusBar = "Getting your data" 
DoEvents 
Loop 

Set htmlANP1 = IE.document 

'Getting the Captcha Password 
Dim strCaptchaPassword As String 
Dim colMyCollection As IHTMLElementCollection 
Set colMyCollection = htmlANP1.getElementById("divQuadro").all 

Dim objLabel As IHTMLElement 

For Each objLabel In colMyCollection 
strCaptchaPassword = strCaptchaPassword & objLabel.innerText 
Next objLabel 

'Getting the input box object and getting it the correct password 
Dim objInputBox As IHTMLElement 
Set objInputBox = htmlANP1.getElementById("txtValor") 
objInputBox.Value = strCaptchaPassword 

'Getting the submit button object and clicking it 
Dim objInputButton As IHTMLElement 
Set objInputButton = htmlANP1.getElementById("image1") 
objInputButton.Click 

'Getting the true rich data HTML 
Set htmlANP1 = IE.document 

'Extracting the data from the html document 
Dim rngValues As range: Set rngValues = Sheet1.range("B17") 
Dim strValues(35) As String 
Dim dblValues(35) As Double 

Dim objElement1 As IHTMLElement 
Set objElement1 = htmlANP1.getElementsByTagName("TABLE")(1) 

Dim colCollection1 As IHTMLElementCollection 
Set colCollection1 = objElement1.all 

Dim intTempCount As Integer 
Dim objTempElement As IHTMLElement 

intTempCount = 32 

For Each objTempElement In colCollection1 
Sheet1.Cells(intTempCount, 3) = objTempElement.tagName 
Sheet1.Cells(intTempCount, 4) = objTempElement.innerText 
intTempCount = intTempCount + 1 
Next objTempElement 
End sub 

回答

0

你是不是等待新的網頁點擊驗證碼按鈕後加載。再次檢查IE的就緒狀態或結束您在這裏的代碼,啓動一個定時器,在X秒內再次啓動您的代碼,然後檢查IE和Document的就緒狀態。

+0

緊接在點擊提交按鈕的代碼之後,我添加了一些代碼來等待10秒,然後循環直到READYSTATE爲COMPLETE。問題仍然存在 –

+0

檢查IE.document的內容,它會改變嗎?你的IE頁面是否真的改變了?如果是,你的網站是否有框架? – darbid

+0

嗨達爾比德。 IE改變了,但是html沒有改變。是的,我只是意識到在HTML中有一個IFrame對象(這是你的意思是幀?)。如何解決這個問題呢? –

0

我在使用iFrame的系統上進行了刮擦,所以使用IE.Readystate並不是很可靠。通常我必須等待另一個元素「存在」,但使用IsObject(元素)也不是很可靠。我必須做的是在我的主代碼中使用一個循環來調用一個函數,所以如果我等待某些東西加載,並且我知道在頁面加載後,有一個ID爲「UserName」的元素,那麼我這樣做..

...

Do Until IsErr(doc, "UserName") = False: Loop 

...

Function IsErr(doc As HTMLDocument, ID As String) As Boolean 

IsErr = True 

On Error GoTo ExitFunction: 

Debug.Print left(doc.getElementById(ID).innerHTML, 1) 
IsErr = False 
Exit Function 

ExitFunction: 

End Function 

我可以做到這一點一直嘗試進行調試循環語句,但是這將是同一個噩夢錯誤處理,所以如果你使用單獨的打印功能,它可以在出錯後退出函數,然後循環重新啓動該功能,它會一直這樣做,直到下一個元素存在。