我有以下 html 部分
<div>
<p class="wholesaletext"> Χονδρική Τιμή: <br/>
<span id="our_price_display" class="price" itemprop="price" content="9.9">
<p class="wholesaletext">9,90 €
</span> χωρίς Φ.Π.Α
<meta itemprop="priceCurrency" content="EUR" />
</p>
</p>
</div>
我正在尝试获取
<p class=wholesaletext">
的文本从上面与以下Set price = ie.Document.getElementsByTagName("span")
Dim lnk As Variant
For Each lnk In price
If lnk.className = "price" Then
wks.Cells(i, "E").Value = lnk.innerText
Exit For
End If
Next
我在细胞中什么也得不到。
我试过了
Set price = ie.Document.querySelector(".price .wholesaletext")
wks.Cells(i, "E").Value = price.innerText
我什么都得不到。
我试过了
Set price = ie.Document.getElementsByTagName("p")
Dim lnk As Variant
For Each lnk In price
If lnk.className = "wholesale" Then
wks.Cells(i, "E").Value = lnk.innerText
Exit For
End If
Next
但它在 html 中得到另一个文本,而不是价格。
最佳答案
获取实际文本的更好方法是使用 .wholesaletext
的 css 类选择器
所以,
ie.document.querySelectorAll(".wholesaletext")
然后按索引访问文本和带有价格的文本
ie.document.querySelectorAll(".wholesaletext").item(0).innerText
ie.document.querySelectorAll(".wholesaletext").item(1).innerText
CSS:
关于excel - 使用 VBA 在 span 标签内的 <p> 元素内获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53812802/