从 Internet Explorer 中提取特定文本

Extract specific text from Internet explorer

我有如下所示的 Html 和 VBA 代码,我正在尝试使用 excel VBA 仅提取名称 Verizon Wireless,所以我使用了以下 excel VBA 代码。但是当我 运行 它返回 "Type:Mobile" 我假设这是因为我使用的 Class 名称由 children 在 div 下共享所以我的问题是,有没有办法只提取承运人的名称?

谢谢

-------HTML CODE-------

<div class="left-phone-info">
          <h1>(405) 482-3431</h1>
          <p class="subtext grey phone-details"><strong>Type:</strong> Mobile</p>
            <p class="subtext grey phone-details">
                <strong>Carrier:</strong> Verizon Wireless
            </p>
        </div>

------VBA CODE-------

Sub get_carrier()
Dim ie As InternetExplorer
Dim ht As HTMLDocument
Dim num As String

num = Range("B1").Value
Set ie = New InternetExplorer
ie.navigate ("https://www.whitepages.com/phone/1-405-482-3431")
ie.Visible = True

Do
DoEvents
Loop Until ie.readyState = 4
Application.Wait Now + TimeSerial(0, 0, 5)


Set ht = ie.document
Set tags = ht.getElementsByClassName("subtext grey phone-details")
For Each tagx In tags
            MsgBox tagx.innerText
        Exit For
    Next

End Sub ```

getElementsByClassName 应该返回一个集合。在您的示例中,它将是一个包含 2 个项目的集合。

尝试通过索引访问第二个项目:

Set tags = ht.getElementsByClassName("subtext grey phone-details")(1)