[英]How to find the title of an URL using VBA in MS Excel
I am working on a VBA project to get Page title and created date from an URL.我正在研究 VBA 项目,以从 URL 获取页面标题和创建日期。 I found this code online but it's not working since IE is not available.
我在网上找到了这段代码,但由于 IE 不可用,所以它不起作用。 Is there any way I can get the title without IE?
有没有什么方法可以在没有 IE 的情况下获得标题?
Function GetPageTitle(a As String)
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = False
.Navigate a
Do Until .ReadyState = 4
DoEvents
Loop
GetPageTitle = .document.Title
.Quit
End With
End Function
I have tried to use Edge as Internet Explorer but it's not working.我曾尝试将 Edge 用作 Internet Explorer,但它不起作用。 I have tried this code also HTML Page Title in Excel VBA
我也试过这个代码HTML Excel VBA 中的页面标题
Option Explicit
Sub GetPageTitleTEST()
Const URL As String = "https://www.google.com"
Dim PageTitle As String: PageTitle = GetPageTitle(URL)
If Len(PageTitle) > 0 Then
Debug.Print PageTitle
Else
Debug.Print "Nope."
End If
End Sub
Function GetPageTitle( ByVal URL As String) As String
Dim Response As String
On Error GoTo ClearError
With CreateObject("MSXML2.ServerXMLHTTP")
.Open "Get", URL, False
.send
Response = .responseText
End With
With CreateObject("htmlfile")
.body.innerHTML = Response
GetPageTitle = .getElementsByTagName("title")(0).innerText
End With
ProcExit:
Exit Function
ClearError:
Debug.Print "'GetPageTitle' Run-time error '" _
& Err.Number & "':" & vbLf & " " & Err.Description
Resume ProcExit
End Function
I followed this other post here Excel VBA using Selenium after following the instructions by @Yu Zhou.在遵循@Yu Zhou 的说明后,我使用 Selenium 在 Excel VBA 中关注了其他帖子。 I was receiving the error about the chrome binary not being found.
我收到有关找不到 chrome 二进制文件的错误。 I switched "edge" to "chrome".
我将“边缘”切换为“铬”。 Basically, the post says you need to download the correct chrome version from here: https://chromedriver.chromium.org/downloads .
基本上,帖子说你需要从这里下载正确的 chrome 版本: https://chromedriver.chromium.org/downloads 。 And then install it (for Windows users) here
C:\Users\ *Username\AppData\Local\SeleniumBasic
.然后在此处安装它(对于 Windows 用户)
C:\Users\ *Username\AppData\Local\SeleniumBasic
。 You need to overwrite the old chrome driver.您需要覆盖旧的 chrome 驱动程序。
If you want to automate Edge with VBA, you need to use SeleniumBasic .如果你想用 VBA 自动化 Edge,你需要使用SeleniumBasic 。 SeleniumBasic is a Selenium based browser automation framework for VB.Net, VBA and VBScript.
SeleniumBasic 是一个基于 Selenium 的浏览器自动化框架,用于 VB.Net、VBA 和 VBScript。
You can follow the steps below:您可以按照以下步骤操作:
C:\Users\%username%\AppData\Local\SeleniumBasic
in my computer (it might also be in this path C:\Program Files\SeleniumBasic
), copy the Edge WebDriver msedgedriver.exe
to this path.C:\Users\%username%\AppData\Local\SeleniumBasic
(它也可能在此路径C:\Program Files\SeleniumBasic
中),将 Edge WebDriver msedgedriver.exe
复制到此小路。msedgedriver.exe
to edgedriver.exe
.msedgedriver.exe
重命名为edgedriver.exe
。Public Sub Selenium()
Dim bot As New WebDriver
bot.Start "edge", "https://www.bing.com"
bot.Get "/"
bot.Wait 5000
Debug.Print bot.Window.Title 'this line of code can get title
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.