简体   繁体   English

Excel Web查询以登录网站

[英]Excel web query to login into a website

I am looking for a macro/vba code to log me into a website which is password protected, there are two password on the site. 我正在寻找一个宏/ vba代码来登录到受密码保护的网站,该网站上有两个密码。 I have got some code which uses internet explorer to get me into website but it does not retrieve data, the web query browser is not logged in, I'm logged in into only internet explorer and not excel web query browser because of that I can not refresh or update data into excel using web query connections which I have already created. 我有一些使用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

So is there a way to change this code from using internet explorer and use excel web query browser so that it can auto log in and I just have to refresh the external connection from web which is already added into my excel sheet? 那么,有没有办法通过使用Internet Explorer和excel Web查询浏览器来更改此代码,使其可以自动登录,而我只需要刷新已添加到我的excel工作表中的Web的外部连接?

PS I am using Excel 2016 64-bit version on windows 10. PS我在Windows 10上使用Excel 2016 64位版本。

You need to get the cookie after logging in in order to identify yourself to the web server in the following requests. 登录后,您需要获取Cookie才能在以下请求中向Web服务器标识自己。 You will always need to log in first and get that variable from the browser. 您将始终需要先登录并从浏览器获取该变量。

After you log in using the code you posted grab the cookies you need (try re-creating what you are trying to do in IExplorer with Network Recorder in the developer tools of the browser. Then you can pass those cookies to the server in the HTTP Post request after closing IE. 使用发布的代码登录后,获取所需的cookie(尝试使用浏览器的开发人员工具中的Network Recorder在IExplorer中重新创建您要尝试的操作。然后,您可以将这些cookie通过HTTP传递给服务器关闭IE后发布请求。

You can use this concept to login to (almost) any website. 您可以使用此概念登录(几乎)任何网站。

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

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM