获取包含字符串的父元素

问题描述 投票:0回答:1

我正在尝试获取

div
元素的类名,该元素包含 url 中许多
div
元素之间的调用区域的信息。由于包含呼叫区域信息的 div 具有美国各州名称,因此我使用一个州名称作为锚点来识别
div

这是我当前的代码(

Add Reference: Tools->Reference and check "Microsoft XML, v3.0"
)

Public Sub Main()
Dim url As String
Dim oHttp As New MSXML2.XMLHTTP
Dim divs As IHTMLElementCollection, div As HTMLDivElement
    
    
    url = "https://www.rebtel.com/en/international-calling-guide/phone-codes/us/"
    oHttp.Open "GET", url, False
    oHttp.send
    
    Dim html As New HtmlDocument
    html.body.innerHTML = oHttp.responseText

    Set divs = html.body.getElementsByTagName("div")
    
    For Each div In divs
        If div.innerHTML Like "*Alabama*" Then
            Debug.Print div.className
        End If
    Next div
    

End Sub

当前输出有几个

divs
包含字符串“Alabama”,因为该站点包含嵌套的 div,并且一个 div 也可以包含字符串“Alabama”及其子元素。

content-wrapper
codes_show_view
container
row gap-l
gap-xl-bottom gap-l-top
pull-left
pull-left
pull-left
pull-left

我想要的输出是带有

classname = gap-xl-bottom gap-l-top

的 div

如何识别包含呼叫区号

div
的特定
classname = gap-xl-bottom gap-l-top

excel vba web-scraping
1个回答
0
投票

你可以这样做,但我不知道它如何推广到不同的网站......

Public Sub Main()
Dim url As String
Dim oHttp As New MSXML2.XMLHTTP
Dim divs As IHTMLElementCollection, div As Object, pDiv As Object
    
    
    url = "https://www.rebtel.com/en/international-calling-guide/phone-codes/us/"
    oHttp.Open "GET", url, False
    oHttp.send
    
    Dim html As New HtmlDocument
    html.body.innerHTML = oHttp.responseText

    Set divs = html.body.getElementsByTagName("div")
    
    For Each div In divs
        If div.innerHTML Like "*Alabama*" And div.className = "pull-left" Then
            Set pDiv = div.parentElement.parentElement 'the element two levels up in the DOM tree
            Debug.Print pDiv.className  '>> gap-xl-bottom gap-l-top
            Exit For
        End If
    Next div
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.