簡體   English   中英

如何從html文件中的JS數組中提取數據到VBA

[英]How to extract data from JS array in an html file to VBA

我正在嘗試在新的實習崗位上自動從在線資源中檢索信息。 我設法創建了可以打開受密碼保護的網站的代碼,並瀏覽另外兩個頁面以轉到所需的頁面。 但是,此頁面上列出了一些信息,表明我在提取信息時遇到了麻煩。 該信息似乎存儲在HTML所訪問的javascript文件中。 有沒有辦法獲取此信息並將其放在excel工作表中?

到目前為止,這是我的代碼...很遺憾,我無法顯示該URL,因為我不確定我的公司是否允許它。

            Sub AutomateIE()

            Dim URL As String
            Dim URL2 As String
            Dim IE As Object
            Dim objPage As Object

            Set IE = CreateObject("InternetExplorer.Application")
            IE.Visible = True 

            URL = "URL" 
            URL2 = "URL2" 
            URL3 = "URL3"
            IE.navigate URL
            Do While IE.ReadyState < 4: 
            DoEvents: Loop
            Do Until IE.ReadyState = 4:
            DoEvents: Loop

            Set objPage = IE.document
            objPage.getelementbyid("ucLogin_txtLogonID").Value = "Employee name" 
            objPage.getelementbyid("ucLogin_txtLogonPassword").Value = "Employee password"
            Application.SendKeys "{Return}", True '
            Application.Wait (Now + TimeValue("0:00:05")) 
            IE.navigate URL2
            Application.Wait (Now + TimeValue("0:00:05"))
            IE.navigate URL3 

            End Sub

如果它是腳本文件中定義的靜態值,則可以使用XMLHTTP60獲取腳本文件的responseText。

如 :

var arr = ["hello", "world"];

假設您想要“世界”

然后將腳本加載到ScriptControl對象中,並對其進行評估:

Dim script As IHTMLScriptElement
Set script = doc.getElementById("myScript") 'may need another way to identify

Dim sControl As ScriptControl 'needs reference to Miscrosoft Script Control 1.0
Set sControl = New ScriptControl
sControl.Language = "JScript"

Dim xhr As MSXML2.XMLHTTP60
Set xhr = New MSXML2.XMLHTTP60
With xhr
     .Open "GET", URL3.Path & "/" & script.src, False 'define full path to script file properly 
     'if necessary change above path to full script src URL
     .send
     Dim jsText As String
     jsText = .responseText
End With
sControl.AddCode jsText 'adds the contents of the script file to ScriptControl object

Dim v As Variant
v = sControl.Eval("arr[1]")

MsgBox v

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM