首页 文章

Excel Web查询登录网站

提问于
浏览
0

我正在寻找一个宏/ vba代码登录到一个受密码保护的网站,网站上有两个密码 . 我有一些代码,使用Internet Explorer进入网站,但它不检索数据,Web查询浏览器没有登录,我只登录到Internet Explorer而不是excel web查询浏览器,因为我可以不使用我已创建的Web查询连接刷新或更新数据到Excel .

Sub WebLogin()
Dim a As String
Set IE = CreateObject("InternetExplorer.Application")
With IE
    .Visible = True
    .Navigate "https://newtrade.sharekhan.com/rmmweb/login/LoginPage.jsp"
    Do Until .ReadyState = 4
        DoEvents
    Loop
    .Document.all.Item("loginid").Value = "myuserid"
    .Document.all.Item("brpwd").Value = "password1"
    .Document.all.Item("trpwd").Value = "password2"
    .Document.forms(0).submit
End With
IE.Quit
End Sub

那么有没有办法改变这个代码使用Internet Explorer并使用excel web查询浏览器,以便它可以自动登录,我只需要刷新已经添加到我的Excel工作表中的Web的外部连接?

P.S我在Windows 10上使用Excel 2016 64位版本 .

2 回答

  • 0

    您需要在登录后获取cookie,以便在以下请求中向Web服务器标识自己 . 您将始终需要先登录并从浏览器中获取该变量 .

    使用您发布的代码登录后,获取您需要的cookie(尝试在浏览器的开发人员工具中使用Network Recorder重新创建您在IExplorer中尝试执行的操作 . 然后,您可以将这些cookie传递给HTTP中的服务器关闭IE后发布请求 .

  • 0

    您可以使用此概念登录(几乎)任何网站 .

    Dim HTMLDoc As HTMLDocument
    Dim oBrowser As InternetExplorer
    Sub Login_2_Website()
    
    Dim oHTML_Element As IHTMLElement
    Dim sURL As String
    
    On Error GoTo Err_Clear
    sURL = "https://www.google.com/accounts/Login"
    Set oBrowser = New InternetExplorer
    oBrowser.Silent = True
    oBrowser.timeout = 60
    oBrowser.navigate sURL
    oBrowser.Visible = True
    
    Do
    ' Wait till the Browser is loaded
    Loop Until oBrowser.readyState = READYSTATE_COMPLETE
    
    Set HTMLDoc = oBrowser.Document
    
    HTMLDoc.all.Email.Value = "sample@vbadud.com"
    HTMLDoc.all.passwd.Value = "*****"
    
    For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
    If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
    Next
    
    ' oBrowser.Refresh ' Refresh If Needed
    Err_Clear:
    If Err <> 0 Then
    Debug.Assert Err = 0
    Err.Clear
    Resume Next
    End If
    End Sub
    

    enter image description here

    enter image description here

相关问题