如何使用宏登录网站并获取整个数据html表

时间:2015-06-25 09:54:25

标签: excel vba excel-vba session excel-2007

我想制作一个首先登录网站然后获取html表数据的宏取件器。我从网站上获取了html表数据,但没有登录页面,但我只获得了excel表中的第一页数据。我需要获取表格的所有页面数据,但首先登录网页。 这是从网站获取html表数据的代码。

Sub Macro1()
'
' Macro1 Macro
'
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;https://datatables.net/examples/basic_init/alt_pagination.html" _
        , Destination:=Range("$A$1"))
        .Name = "alt_pagination.html"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSelectedTables
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
    ActiveWindow.SmallScroll Down:=-36
End Sub

1 个答案:

答案 0 :(得分:0)

我已经单独完成了这个。第一个宏将通过IE登录到网站,第二个将获得整个html表数据。

     Sub Login()   
             Set ie = CreateObject("InternetExplorer.application")
            ie.Visible = True
           ie.Navigate ("https://www.facebook.com/?id=" & ActiveCell) 'Enter your website url
            Do
                If ie.ReadyState = 4 Then
                    ie.Visible = True
                    Exit Do
                Else
                    DoEvents
                End If
            Loop
            ie.document.forms(0).all("opername").Value = "myuser" 'Enter UserName
            ie.document.forms(0).all("password").Value = "mypass" 'Enter Password
            ie.document.forms(0).submit

               End With
        End Sub

Sub Macro1()
  With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;https://datatables.net/examples/basic_init/alt_pagination.html" _
        , Destination:=Range("$A$1"))
        .Name = "alt_pagination.html"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSelectedTables
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
End Sub