用于登录网站的Excel网页查询

时间:2017-10-15 10:29:44

标签: excel excel-vba vba

我正在寻找一个宏/ 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>

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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