简体   繁体   English

如何在 MS Excel 中使用 VBA 找到 URL 的标题

[英]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 中的页面标题

Get Webpage Title获取网页标题

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:您可以按照以下步骤操作:

  1. Download the latest version of SeleniumBasic v2.0.9.0 from this link and install it.从此链接下载最新版本的 SeleniumBasic v2.0.9.0 并安装。
  2. Download the corresponding version of Edge WebDriver from this link .从此链接下载相应版本的 Edge WebDriver。
  3. Find the path of SeleniumBasic which is 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.在我的计算机中找到 SeleniumBasic 的路径C:\Users\%username%\AppData\Local\SeleniumBasic (它也可能在此路径C:\Program Files\SeleniumBasic中),将 Edge WebDriver msedgedriver.exe复制到此小路。
  4. Rename msedgedriver.exe to edgedriver.exe .msedgedriver.exe重命名为edgedriver.exe
  5. Open Excel and write the VBA code.打开 Excel 并编写 VBA 代码。
  6. In the VBA code interface, click Tools > References , add Selenium Type Library reference and click OK to save.在 VBA 代码界面,点击Tools > References ,添加Selenium Type Library引用并点击OK保存。
  7. Sample VBA code to automate Edge using SeleniumBasic to get URL title:使用 SeleniumBasic 自动化 Edge 以获取 URL 标题的示例 VBA 代码:
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.

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