我試圖從網頁的一部分中取消數據。爲了進入該部分,我需要填寫一個驗證碼安全代碼並點擊一個按鈕,但這是正確的,因爲安全代碼實際上是寫在頁面的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
緊接在點擊提交按鈕的代碼之後,我添加了一些代碼來等待10秒,然後循環直到READYSTATE爲COMPLETE。問題仍然存在 –
檢查IE.document的內容,它會改變嗎?你的IE頁面是否真的改變了?如果是,你的網站是否有框架? – darbid
嗨達爾比德。 IE改變了,但是html沒有改變。是的,我只是意識到在HTML中有一個IFrame對象(這是你的意思是幀?)。如何解決這個問題呢? –