gpt4 book ai didi

html - 在两个或多个条件下搜索内部文本

转载 作者:行者123 更新时间:2023-12-04 21:53:59 25 4
gpt4 key购买 nike

我正在从 excel VBA 进行谷歌搜索。我有兴趣提取的文本位于 span 标签内:

 <div class="f kv_Swb" style="white-space:nowrap">
...
<span class="st">
<span class="f">no relevant text</span>
this is the text it matters, it has a keyword i need
</span>
</div>

有许多嵌套的 div 标签。

它是元素类中的字符串 st , 但在元素类 f 之外.正如我所说,我使用了这样的 VBA 脚本:
 Dim IE as Object
Dim doc as Object
Dim elementA as Object
Dim elementB as Object
Dim TagA as Object
Dim TagB as Object

Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://www.unsuspectwebpage.com/about"
Set doc = IE.Document

Do Until IE.ReadyState = 4
DoEvents
Loop

Set TagA = doc.getElementsByTagName("span")
For Each elementA In TagA
Set TagB = doc.getElementsByClassName("st")
For Each elementB In TagB
ws.Range("A1") = ws.Range("A1") & elementB.innertext
Next elementB
Next elementA

如何获取类中的文本 st但课外 f ?

最佳答案

不是一个非常有效的,但它应该为您获取所需的内容:

Dim elem As Object, HTML As New HTMLDocument

For Each elem In HTML.getElementsByClassName("st")
Debug.Print Split(elem.innerText, elem.getElementsByTagName("span")(0).innerText)(1)
Next elem

输出:
this is the text it matters, it has a keyword i need

关于html - 在两个或多个条件下搜索内部文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48725184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com