【问题标题】:Cannot get the text of nth span element using vba无法使用 vba 获取第 n 个跨度元素的文本
【发布时间】:2020-02-08 13:28:36
【问题描述】:

我有以下 html 部分

<div class="description">
<span>Brand:</span> 
<a href="http://www.bestfamily.gr">Nikon</a><br/>
<span>Product Code:</span> 130342 <br/>
<span>Barcode</span> 18208948581 <br/>
<span>Availability:</span>Available</div>

我正在尝试使用以下内容获取最后一个跨度和可用一词

Set availability = ie.Document.getElementsByClassName(".description").getElementsByTagName("span")(2)
wks.Cells(i, "D").Value = availability.innerText

但它显示所有跨度文本 我在这里做错了什么?

【问题讨论】:

  • 我对 HTML 和 VBA 不是很熟悉。也许考虑这个问题以供参考:stackoverflow.com/questions/42741997/get-all-innertext-vba
  • 尝试将ie.Document.getElementsByClassName(".description").. 更改为ie.Document.getElementsByClassName("description")..。从 description 的前面移除 .
  • 它不工作它在单元格中没有显示任何内容
  • 尝试使用元素集合,然后使用它的属性进行计数

标签: excel vba web-scraping


【解决方案1】:

descendant combination 中使用last-child css 伪类和父元素class selector

.description span:last-child

:last-child CSS 伪类表示一个元素中的最后一个元素 兄弟元素组。

申请:

单场比赛

Set availability = ie.document.querySelector(".description span:last-child")
Cells(1,1) = availability.innerText

所有匹配

Set availability = ie.document.querySelectorAll(".description span:last-child")
Cells(1,1) = availability.item(0).innerText

否则,您可以从该父类返回 span 集合并对其进行索引

Set availability = ie.document.querySelectorAll(".description span")
Cells(1,1) = availability.item(2).innerText '<==choose your index here

甚至链:

Set availability = ie.document.querySelector(".description span + span + span")  '<==expand as required. This uses [adjacent sibling combinator][4].

遗憾的是,伪类 nth-of-type / nth-child 在 VBA 实现中不受支持,尽管您可以使用许多其他语言,例如蟒蛇。

——

如果仅在 Available 之后,您应该能够使用 .description 作为选择器来返回 div 中的所有文本。然后使用 Chr$(32) 在 .innerText 上使用 Split 来分割并提取 UBound(即生成数组的最后一个元素)

Set availability = ie.document.querySelector(".description")
Dim arr() As String
arr = split( availability.innerText, ":")
Cells(1,1) = arr(UBound(arr))

【讨论】:

  • 是的,但我只得到了Availability: inside span这个词,而不是属于div的文本Available?我不确定
  • @Nikos 按类名选择 div。拆分 .innertext 并取 ubound
  • 你的意思是 Set availability = ie.document.querySelectorAll(".description div") Cells(1,1) = availability.innertext.unbound ?
  • 而不是 chr$(32) 我只是放了“:”,我得到了最后一个字,非常感谢
【解决方案2】:

正如 Zac 在 cmets 中指出的那样,您不应该在 getElementsByClassName 方法中使用句点 .

ie.Document.getElementsByClassName 正在返回一个 DispHTMLElementCollection 元素。您需要指定要引用的元素

设置可用性 = ie.Document.getElementsByClassName(".description")(0).getElementsByTagName("span")(2)

编写代码的更好方法是引用 Microsoft HTML 对象库并创建一个变量来测试返回的每个元素。不幸的是,DispHTMLElementCollection 实现中有一个错误,因此您需要使用 Object 而不是 DispHTMLElementCollection

Dim doc As HTMLDocument
Dim availability As Object
Set doc = ie.Document
Set availability = doc.getElementsByClassName("description")

Dim div As HTMLDivElement
Dim span As HTMLSpanElement
Dim spans As Object

For Each div In availability
    Set spans = div.getElementsByTagName("span")
    For Each span In spans
        Debug.Print span.innerText
    Next
Next    

输出

【讨论】:

  • 是的,但我想要的不是上面的标题,而是最后一个字可用
猜你喜欢
  • 1970-01-01
  • 2019-04-11
  • 2012-02-02
  • 1970-01-01
  • 2018-08-07
  • 2010-10-16
  • 2020-04-24
  • 2021-07-14
  • 1970-01-01
相关资源
最近更新 更多