Vba 登录网站后获取网络数据

Vba 登录网站后获取网络数据,vba,excel,Vba,Excel,我正在尝试从一个需要登录用户和密码的网站获取数据。我已经跟踪并成功登录了该网站,但由于某些原因,该网站无法获得该表 代码如下: 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 Inter

我正在尝试从一个需要登录用户和密码的网站获取数据。我已经跟踪并成功登录了该网站,但由于某些原因,该网站无法获得该表

代码如下:

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
Sub-GetTable()
Dim ieApp作为InternetExplorer
Dim ieDoc作为对象
可作为对象的
将剪辑变暗为数据对象
'创建ie的新实例
设置ieApp=新的InternetExplorer
您不需要这个,但它有助于调试
ieApp.Visible=True
'假设我们没有登录,直接进入登录页面
ieApp.Navigate“https://accounts.google.com/ServiceLogin"
在ieApp.Busy:DoEvents:Loop时执行
直到ieApp.ReadyState=ReadyState\u完成:DoEvents:Loop
设置ieDoc=ieApp.Document
'填写登录表单–
使用ieDoc.forms(0)
.Email.Value=”email@email.com"
.Passwd.Value=“密码”
提交
以
在ieApp.Busy:DoEvents:Loop时执行
直到ieApp.ReadyState=ReadyState\u完成:DoEvents:Loop
现在我们进入了,进入我们想要的页面
ieApp.浏览“我的网站.com”
在ieApp.Busy:DoEvents:Loop时执行
直到ieApp.ReadyState=ReadyState\u完成:DoEvents:Loop
'根据表的id获取表
设置ieDoc=ieApp.Document
对于i=0到(ieDoc.all.Length-1)
“只看桌子
如果TypeName(ieDoc.all(i))=“HTMLTable”,则
设置ieTable=ieDoc.all(i)
'我想检查第三行(.Rows(2)),如果有错误,将得到一个错误
“不到三排。
如果ieTable.Rows.Length>2,则
“这是第三行第一个单元格中的文本,它告诉我
“我的桌子是对的
如果ieTable.Rows(0).Cells(0).innertext=“Text”则
'将表格html复制到剪贴板并粘贴到工作表
如果不可否认,那就什么都不是了
Set clip=新数据对象
clip.SetText“&ieTable.outerHTML&”
夹板
表1.选择
表1.范围(“A1”)。选择
Sheet1.1特殊的“Unicode文本”
如果结束
如果结束
如果结束
如果结束
接下来我
“关闭”er
退出
设置ieApp=Nothing
端接头

假设正确地标记了
标记,则可以使用以下方法获取表集合,然后循环使用:

ieDoc.getElementsByTagName("table")

我已经编辑了你的标题。请参阅“”,其中共识是“不,他们不应该”。re:“这是第三行第一个单元格中的文本”。看起来您正在检索第一行中的第一个单元格(单元格和行的索引号都是从零开始的)。尝试
ieTable.Rows(2).Cells(0).innertext
。尝试将
的“HTMLTable”
更改为
的“HTMLTable”
。使用
选项Explicit
在最开始时,代码不会编译。问题解决了。我照迪伊说的做了,结果成功了。谢谢!