繁体   English   中英

Excel VBA提取咏叹调标签值

[英]Excel VBA extracting aria-label value

我正在尝试提取aria-label属性内的文本,但是我似乎无法正常工作。 我可以使用相同的代码提取href值,因此我认为它也可能起作用。 任何帮助,将不胜感激。

我使用的网址是https://www.facebook.com/marketplace/item/328932021226229 截图:

 <div class="_3-8z"> <div> <span class="_3ziq">Seller Information</span> <div class="clearfix" direction="left"> <div class="_ohe lfloat"> <div> <a class="img _8o _8t" aria-label="John Smith, View seller profile" href="#" data-hovercard="/ajax/hovercard/user.php?id=100002935356728&amp;extragetparams=%7B%22hc_location%22%3A%22marketplace_hovercard%22%2C%22existingThreadID%22%3Anull%2C%22forSaleItemID%22%3A%22328932021226229%22%2C%22name%22%3A%22Zsigmond%20Lali%22%7D" modalProps="[object Object]" profileID="100002935356728" resource="[object Object]"> 

    Sub Macro2()

marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
    On Error Resume Next    ' sometimes more web pages are counted than are open
    my_url = objShell.Windows(x).document.Location
    my_title = objShell.Windows(x).document.Title

    If my_title Like "Marketplace" & "*" Then 'compare to find if the desired web page is already open
        Set IE = objShell.Windows(x)
        marker = 1
        Exit For
    Else
    End If
Next

        Dim aNodeList As Object, i As Long
        Set aNodeList = IE.document.querySelectorAll(".img _8o _8t[aria-label]")
        For i = 0 To aNodeList.Length - 1
            ActiveSheet.Cells(i + 2, 2) = aNodeList.Item(i)
        Next

End Sub

您的代码段链接HTML至少在我看来没有出现在链接中。 另外,如果您位于显示的aria-label之后,则CSS选择器的显示选择器类的类选择器错误。

这个

._3cgd[aria-label]

查找具有aria-label属性的类名._3cgd的元素。 您的摘要中没有一个。

我希望但由于上述原因而无法正确测试,您可以使用getAttribute ,但如果分割目标元素的.outerHTML失败。

以下是根据您显示的代码段更笼统的选择器。 您可能需要针对HTML进行调整。 getAttribute只用您的代码片段生成了null ,但是我不确定如果实时页面的语法正确,它的行为是否会有所不同。 outerHTML拆分返回John Smith, View seller profile

With IE.document.querySelector("a[class='img _8o _8t'][profileid='100002935356728']")
   Debug.Print .getAttribute("aria-label")
   Debug.Print Split(Split(.outerHTML, "aria-label=" & Chr$(34))(1), Chr$(34))(0)
End With

我只使用querySelector并使用profileid定位,以更普遍地使用带有aria-label的所有匹配类:

Dim eles As Object, i As Long
Set eles = IE.document.querySelectorAll("a[class='img _8o _8t'][aria-label]")
For i = 0 To eles.Length - 1
    With eles.item(i)
        Debug.Print .getAttribute("aria-label")
        Debug.Print Split(Split(.outerHTML, "aria-label=" & Chr$(34))(1), Chr$(34))(0)
    End With
Next

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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