excel - Excel VBA webscrape,我如何获得跨度值?
问题描述
相对较新的 VBA 和新的网络抓取。我的任务是从网站获取一些数据。我试过在这里搜索帮助,根据我发现的内容尝试了很多排列,但没有得到我需要的结果。网页 DOM Explorer 的片段(使用“F12 开发者工具”)显示以下内容(经过编辑使其通用):
<div class=”nav nav-list”>
<div>
<span class=”nav-list-item”>Item:</span>
<span>
mySearchString and other text
</span>
</div>
<div>…</div>
<div>
<span class=”nav-list-item”>Retail UPC:</span>
<span>upcNumber</span>
</div>
<div>…</div>
</div>
</div>
我正在尝试搜索“mySearchString”,提取“和其他文本”并搜索“零售 UPC:”并提取“upcNumber”。
尝试使用嵌套的 if 语句,但无法正常工作。以下是我一直在玩的最新版本的片段:
Dim harborDesc() as String
Dim ieObj As InternetExplorer
Set ieObj = CreateObject("InternetExplorer.Application")
Dim htmlEle As Object
Dim itemurl As String
Itemurl = “url of interest”
ieObj.navigate itemurl 'in this case, the web page is has the same name as the itemNum
Do While ieObj.readyState <> READYSTATE_COMPLETE 'wait by repeating loop until ready
Loop
For Each htmlEle In ieObj.document.getElementsByClassName("nav-list-item")
harborDesc = Split(htmlEle.innerText, htmlEle.getElementsByTagName("span")(1).innerText)
Next htmlEle
提前感谢任何提示/帮助
解决方案
您可以设置一个 nodeList 并循环它们以寻找您的搜索词。
nodeList 是从带有 Or 语法的 css 查询生成的,这意味着您将获得
<span class="nav-list-item">
但也匹配那些跨标签相邻兄弟元素,例如
<span class="nav-list-item">Retail UPC:</span>
<span>upcNumber</span>
您使用Instr
来.innerText
匹配您的第一个搜索词。然后,如果找到,请使用Replace
删除匹配的文本并保留问题中指定的其余部分。
如果您Retail UPC
在给定索引处找到,则upcNumber
应该在下一个索引处。
VBA:
Option Explicit
Public Sub FindInfo()
Const SEARCH_TERM1 As String = "mySearchString"
Const SEARCH_TERM2 As String = "Retail UPC:"
Dim html As HTMLDocument, searchTermCandidates As Object
Dim i As Long, index As Long, ieObj As InternetExplorer
Set ieObj = New InternetExplorer
With ieObj
.Visible = True
.Navigate2 "url"
While .Busy Or .readyState < 4: DoEvents: Wend
Set html = .document
Set searchTermCandidates = html.querySelectorAll("span.nav-list-item, span.nav-list-item + span")
For i = 0 To searchTermCandidates.Length - 1
If InStr(searchTermCandidates.item(i).innerText, SEARCH_TERM1) > 0 Then
Debug.Print Replace$(searchTermCandidates.item(i).innerText, SEARCH_TERM1, vbNullString)
End If
If searchTermCandidates.item(i).innerText = SEARCH_TERM2 Then
Debug.Print searchTermCandidates.item(i + 1).innerText
End If
Next
.Quit
End With
End Sub
推荐阅读
- javascript - 使用 Jest 进行 tryCatch 单元测试
- html - 使用 YAML 选项“包括:”和“in_head”的 RMarkdown 头标记中的 Google Analytics 代码存在问题
- python - 我如何从这个网站(https://www.mohfw.gov.in/)中提取印度冠状病毒病例的州数据(州名、受影响和死亡)
- git - 如何在不更改提交 ID (SHA) 的情况下将单个提交从一个分支合并到另一个分支
- python - 根据某些条件对文本文件数据进行分组
- r - R - 使用带有 ggplot 的网格路径
- sql - sql oracle group by 和条件
- reactjs - 如何包装来自 React useState 钩子的 setValue 调用并保持相同的类型签名?
- haskell - func = elem [1..10] 在 GHCi 中工作但不能编译
- python - 在python中使用名字和姓氏生成唯一的用户名