在excel vba中使用IE浏览器打开指定网页,获取网页中元素的值
Function NavigateGetToken() As String Dim ResultToken As String Dim LoginURL As String Dim objIE As InternetExplorer On Error GoTo MyErr LoginURL = "https://www.baidu.com" ' 你想打开的网址 Set objIE = CreateObject("InternetExplorer.Application") With objIE .Visible = True .Silent = False .Navigate LoginURL ' 页面是否加载完成 Do While objIE.Busy = True Or objIE.ReadyState <> 4 DoEvents Loop Do ' The token page is open If InStr(1, objIE.LocationURL(), LoginURL) > 0 Then ' Get the token value ResultToken = objIE.Document.getElementById("token").innerHTML Exit Do End If Loop While InStr(1, objIE.LocationURL(), LoginURL) <= 0 End With NavigateGetToken = ResultToken Exit Function MyErr: NavigateGetToken = "" ' Error returned to empty End Function