【问题标题】:Excel VBA extracting href valueExcel VBA提取href值
【发布时间】:2026-01-06 09:05:02
【问题描述】:

我有一个宏,它试图从页面中提取所有 href 值,但它似乎只获取第一个。如果有人可以帮助我,将不胜感激。

我使用的网址是https://www.facebook.com/marketplace/vancouver/entertainment

Screenshot of HTML

<div class="_3-98" data-testid="marketplace_home_feed">
  <div>
    <div>
      <div class="_65db">
          <a class="_1oem" href="/marketplace/item/920841554781924" data-testid="marketplace_feed_item">
          <a class="_1oem" href="/marketplace/item/580124349088759" data-testid="marketplace_feed_item">
          <a class="_1oem" href="/marketplace/item/1060730340772072" data-testid="marketplace_feed_item">
    Sub Macro1()
``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 "Facebook" & "*" 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

Set my_data = ie.document.getElementsByClassName("_3-98")
Dim link
i = 1
For Each elem In my_data
    Set link = elem.getElementsByTagName("a")(0)
    i = i + 1

     'copy the data to the excel sheet
    ActiveSheet.Cells(i, 4).Value = link.href

Next

End Sub

【问题讨论】:

  • 请使用提供的sn-p工具插入html代码。我们无法复制和测试图像。另外,可以分享一下网址吗?
  • 添加了 html 代码和 URL

标签: html excel vba web-scraping href


【解决方案1】:

您可以使用 CSS 选择器组合来获取元素。如果您提供实际的 HTML,而不是图像,则更容易测试和确定最佳组合。选择器通过querySelectorAll 方法应用以返回所有匹配元素的nodeList。您遍历nodeList.Length 以按索引从0.Length-1 访问项目。

VBA:

Dim aNodeList As Object, i As Long
Set aNodeList = ie.document.querySelectorAll("._1oem[href]")
For i = 0 To aNodeList.Length-1
   Activesheet.Cells(i + 2,4) = aNodeList.item(i)
Next 

css选择器组合为._1oem[href],选择_1oem类的元素的href属性。 "." 是一个类选择器,[] 是一个属性选择器。这是一个fast 和健壮的方法。

以上假设没有父 form/frame/iframe 标签可以协商。

匹配两个属性而不是类的替代选择器是:

html.querySelectorAll("[data-testid='marketplace_feed_item'][href]")

完整示例:

Option Explicit
Public Sub GetInfo()
    Dim IE As New InternetExplorer
    With IE
        .Visible = True
        .navigate "https://www.facebook.com/marketplace/vancouver/entertainment"

        While .Busy Or .readyState < 4: DoEvents: Wend

        Dim aNodeList As Object, i As Long
        Set aNodeList = IE.document.querySelectorAll("._1oem[href]")
        For i = 0 To aNodeList.Length - 1
            ActiveSheet.Cells(i + 2, 4) = aNodeList.item(i)
        Next
        'Quit '<== Remember to quit application
    End With
End Sub

【讨论】:

  • 嘿,感谢您的帮助,但它看起来不像 ie.document.querySelectorAll("._1oem[href]") 或 html.querySelectorAll("[data-testid='marketplace_feed_item'] [href]") 正在返回任何内容
  • 请尝试上面给出的完整示例。它似乎需要 .item(i) 语法。
  • 谢谢!效果很好
【解决方案2】:

您只要求每个具有 _3-98 类的元素中的第一个锚元素。遍历父元素中的锚元素集合。

...

dim j as long
Set my_data = ie.document.getElementsByClassName("_65db")

For Each elem In my_data

    for i = 0 to elem.getelementsbytagname("a").count -1

        j = j+1
        ActiveSheet.Cells(j, 4).Value = elem.getElementsByTagName("a")(i).href

    next i

Next elem 

...

【讨论】:

  • 谢谢,我明白我哪里出错了。不幸的是,它仍然只给了我第一个元素。