对于VBA和Web抓取来说相对较新。我的任务是从网站上获取一些数据。我尝试在这里搜索以寻求帮助,根据发现的内容尝试了许多排列,但未获得所需的结果。 网页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
在此先感谢您的提示/帮助
答案 0 :(得分:0)
htmlEle.getElementsByTagName("span")(1)
可能试图返回只有1个元素的数组,因此该数组的唯一可返回值将位于数组htmlEle.getElementsByTagName("span")(0)
的第一位置。 / p>
此外,您使用Split()
的方式对我来说也没有意义。 Split()
的参数为Split(expression, [ delimiter, [ limit, [ compare ]]])
,这意味着您要寻找的.innertext
元素的<span>
是分隔符?另外,我没有看到harborDesc
首先被定义为适当大小(或任何大小)的数组的任何地方,这很可能就是您的Error 91
出现的原因,例如{{1} }需要一个数组才能接受值。
编辑:
从我的评论中添加。
如果您想要的是Split()
内部文本中的文本,则应该执行
<span>
编辑2:
如果在查找“ span”元素时遇到问题,也许您正在寻找一个匹配的“ nav-list-item”类,其中没有“ span”元素。在这种情况下,值得创建一个嵌套的Dim harborDesc() As String
Redim harborDesc(0 to 1)
For Each htmlEle In ieObj.document.getElementsByClassName("nav-list-item")
Redim Preserve harborDesc(0 to UBound(harborDesc) + 1)
harborDesc(UBound(harborDesc) - 1) = htmlEle.getElementsByTagName("span")(0).innerText
Next htmlEle
来进行测试,例如
For-Loop
答案 1 :(得分:0)
您可以设置一个nodeList并使它们循环搜索您的搜索字词。
nodeList是通过使用Or语法的css查询生成的,这意味着您将获得
<span class="nav-list-item">
但还要匹配跨度标签adjacent sibling等元素,例如
<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