我想從一個網站抓取一些足球運動員數據來填充一個私人使用的數據庫。我已經包含了下面的整個代碼。第一部分是調用第二個函數來填充數據庫的活套。我已經在MSAccess中運行這個代碼,去年夏天填充了一個數據庫,它運行得非常好。VBA掛在ie.busy和readystate檢查
現在我只得到了幾支球隊,以填補該計劃被在
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
掛了之前我已經搜索關於此錯誤的無數的網站,並試圖通過把在子功能等待一個改變這種代碼幾秒鐘或其他工作週期。這些都不能解決問題。我也試過在多臺電腦上運行。
第一臺電腦通過3支隊伍(或第二個功能的三個呼叫)。第二臺速度較慢的電腦通過5支隊伍。兩個最終都會掛起第一臺電腦有Internet Explorer 10,第二臺電腦有IE8。
Sub Parse_NFL_RawSalaries()
Status ("Importing NFL Salary Information.")
Dim mydb As Database
Dim teamdata As DAO.Recordset
Dim i As Integer
Dim j As Double
Set mydb = CurrentDb()
Set teamdata = mydb.OpenRecordset("TEAM")
i = 1
With teamdata
Do Until .EOF
Call Parse_Team_RawSalaries(teamdata![RotoworldTeam])
.MoveNext
i = i + 1
j = i/32
Status ("Importing NFL Salary Information. " & Str(Round(j * 100, 0)) & "% done")
Loop
End With
' reset variables
teamdata.Close
Set teamdata = Nothing
Set mydb = Nothing
Status ("") 'resets the status bar
End Sub
Seconnd功能:
Function Parse_Team_RawSalaries(Team As String)
Dim mydb As Database
Dim rst As DAO.Recordset
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim TABLEelements As IHTMLElementCollection
Dim TRelements As IHTMLElementCollection
Dim TDelements As IHTMLElementCollection
Dim TABLEelement As Object
Dim TRelement As Object
Dim TDelement As HTMLTableCell
Dim c As Long
' open the table
Set mydb = CurrentDb()
Set rst = mydb.OpenRecordset("TempSalary")
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.navigate "http://www.rotoworld.com/teams/contracts/nfl/" & Team
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set HTMLdoc = IE.Document
Set TABLEelements = HTMLdoc.getElementsByTagName("Table")
For Each TABLEelement In TABLEelements
If TABLEelement.id = "cp1_tblContracts" Then
Set TRelements = TABLEelement.getElementsByTagName("TR")
For Each TRelement In TRelements
If TRelement.className <> "columnnames" Then
rst.AddNew
rst![Team] = Team
c = 0
Set TDelements = TRelement.getElementsByTagName("TD")
For Each TDelement In TDelements
Select Case c
Case 0
rst![Player] = Trim(TDelement.innerText)
Case 1
rst![position] = Trim(TDelement.innerText)
Case 2
rst![ContractTerms] = Trim(TDelement.innerText)
End Select
c = c + 1
Next TDelement
rst.Update
End If
Next TRelement
End If
Next TABLEelement
' reset variables
rst.Close
Set rst = Nothing
Set mydb = Nothing
IE.Quit
End Function
如果'IE.Quit'設法有效關閉所有IE應用程序,則檢入系統應用程序管理器。您可以嘗試只打開一個IE應用程序並將其作爲參數傳遞給您的函數。以我的經驗打開IE是耗時的過程... –