我正在尝试从需要登录用户和密码的网站获取数据。我已经关注this tutorial并设法登录该网站,但由于某种原因,它没有得到表格。
以下是代码:
Sub GetTable()
Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim ieTable As Object
Dim clip As DataObject
'create a new instance of ie
Set ieApp = New InternetExplorer
'you don’t need this, but it’s good for debugging
ieApp.Visible = True
'assume we’re not logged in and just go directly to the login page
ieApp.Navigate "https://accounts.google.com/ServiceLogin"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Set ieDoc = ieApp.Document
'fill in the login form –
With ieDoc.forms(0)
.Email.Value = "email@email.com"
.Passwd.Value = "password"
.submit
End With
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
'now that we’re in, go to the page we want
ieApp.Navigate "my-website.com"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
'get the table based on the table's id
Set ieDoc = ieApp.Document
For i = 0 To (ieDoc.all.Length - 1)
'Only look at tables
If TypeName(ieDoc.all(i)) = “HTMLTable” Then
Set ieTable = ieDoc.all(i)
'I want to check the 3rd row (.Rows(2)) and will get an error if there
'are less than three rows.
If ieTable.Rows.Length > 2 Then
'Here’s the text in the first cell of the third row that tells me
'I have the right table
If ieTable.Rows(0).Cells(0).innertext = "Text" Then
'copy the tables html to the clipboard and paste to teh sheet
If Not ieTable Is Nothing Then
Set clip = New DataObject
clip.SetText "<html>" & ieTable.outerHTML & "</html>"
clip.PutInClipboard
Sheet1.Select
Sheet1.Range("A1").Select
Sheet1.PasteSpecial "Unicode Text"
End If
End If
End If
End If
Next i
'close 'er up
ieApp.Quit
Set ieApp = Nothing
End Sub
答案 0 :(得分:0)
假设正确标记了table
标签,您可能已经使用以下方法来获取表的集合,然后可以循环浏览这些表:
ieDoc.getElementsByTagName("table")