简体   繁体   English

使用 Excel-VBA 获取 HTML 源 - 弹出 Window

[英]Getting HTML Source with Excel-VBA - Pop Up Window

The post below has a method to extract the HTML of a url using the following code.下面的帖子有一种使用以下代码提取 url 的 HTML 的方法。

Getting HTML Source with Excel-VBA 使用 Excel-VBA 获取 HTML 源

    Public Function getHTTP(ByVal url As String) As String
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", url, False: .Send
        getHTTP = StrConv(.responseBody, vbUnicode)
    End With
End Function

The code works great, but my issue is that the website that I have (an internal website) has a pop-up window the first time I log into it.代码运行良好,但我的问题是我拥有的网站(内部网站)在我第一次登录时弹出 window。 So accessing through this method gets me the HTML of the pop-up.所以通过这种方法访问得到了弹出窗口的 HTML。 How can I get beyond this window?我怎样才能超越这个 window?

在此处输入图像描述

When operating the website manually, the first time I access the web page, the pop-up appears for a few seconds then it disappears and I have click on a button to login.手动操作网站时,第一次访问web页面时,弹窗出现几秒钟然后消失,我点击了一个按钮登录。


Update: Taking a deeper dive, the html returned is the login window for the site.更新:深入研究,返回的 html 是该站点的登录 window。 Normally on this intranet based site, my first access in the day, I click a 'continue' button (no username/password) and it auto logs me in. And any time I access the site moving forward I can go directly to parts of the site without re-logging in. The link that I am using as my url is a direct link to a certain page on the site.通常在这个基于 Intranet 的站点上,我当天第一次访问时,我单击“继续”按钮(没有用户名/密码),然后它会自动登录。并且任何时候我继续访问该站点时,我都可以 go 直接访问该站点无需重新登录。我用作 url 的链接是指向该站点上某个页面的直接链接。 Curious on how to get XMLHTTP logged into the site...很好奇如何让 XMLHTTP 登录到网站...

在此处输入图像描述

Here is the login - it is not a standard username and password.这是登录信息 - 它不是标准的用户名和密码。 It is a username, password, department and city.它是用户名、密码、部门和城市。 Also a radio button to toggle between 2 settings.还有一个单选按钮,用于在 2 个设置之间切换。 Then I would click on login.然后我会点击登录。

I would firstly try this small modification, in order to exactly obtain the responseBody of the intermediary page (only one time):我会先尝试这个小修改,以便准确获取中间页面的responseBody (仅一次):

Public Function getHTTP(ByVal url As String) As String
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", url, False: .send
        Debug.print StrConv(.responseBody, vbUnicode):getHTTP = ""
    End With
End Function

And then I would copy the response from Immediate Window and use it next adapted code:然后我会复制来自Immediate Window的响应并使用下一个改编代码:

Public Function getHTTP(ByVal url As String) As String
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", url, False: .send

        Do While StrConv(.responseBody, vbUnicode) = "the copied text from Immediate Window"
            DoEvents
        Loop
        getHTTP = StrConv(.responseBody, vbUnicode)
    End With
End Function

Or, if the response is too long, use Left("the text in discussion", 10) = "Whatever x"或者,如果响应太长,请使用Left("the text in discussion", 10) = "Whatever x"

Edited: Try transforming of your initial function in the next way, to include authentication:编辑:尝试以下列方式转换您的初始 function 以包括身份验证:

Public Function getHTTP(ByVal url As String) As String
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", url, False: .Send
        .setRequestHeader "Authorization", "Basic " & Base64Encode(Your_User & ":" & password)
        getHTTP = StrConv(.responseBody, vbUnicode)
    End With
End Function

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

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