根据this post我修复了对象检查器。有时代码可以正常运行10个条目,使它们都正确,有时它会运行5个。有时它会使条目错误。
在获取元素的非文本时总是会失败。如果Y / N结果错误,我根本不知道是什么导致了这一点。
请帮忙!这让我很生气。我一遍又一遍地检查每个阶段的错误。
Sub LetsAutomateIE()
Dim barcode As String
Dim rowe As Integer
Dim document As HTMLDocument
Dim Element As HTMLDivElement
Dim text As String
Dim pos As Integer
Set ie = CreateObject("InternetExplorer.Application")
rowe = 2
While Not IsEmpty(Cells(rowe, 2))
barcode = Cells(rowe, "B").Value
pos = 0
text = ""
Set document = Nothing
With ie
.Visible = False
.navigate2 "https://www.amazon.co.uk/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords=" & barcode
Do Until ie.readyState = 4
Loop
End With
Set document = ie.document
If IsObject(document.getElementById("result_0")) = False Then GoTo Here
text = document.getElementById("result_0").innerText
If InStr(text, "STEELBOOK") Or InStr(text, "Steelbook") Or InStr(text, "Steel book") <> 0 Then pos = 1
If pos <> 0 Then Cells(rowe, 4) = "Y" Else Cells(rowe, 4) = "N"
Here:
rowe = rowe + 1
Wend
Set ie = Nothing
End Sub
以下是我正在使用的一系列样本条形码。我从来没有成功地完成这些工作。
5030305517076
5030305517816
5060223767925
5060223767949
5060223767956
5060223767970
5060223767994
8717418358563
8717418365851
非常感谢你,
萨姆
答案 0 :(得分:3)
一个问题是,对于某些条形码,没有找到任何结果。
如果您使用IE.Visible = true
测试代码,则会看到如下文字:
您的搜索&#34; 5060223767949&#34;与任何产品都不匹配。
另一个问题是条件IsObject(document.getElementById("result_0")) = False
。这不能很好地工作,因为IsObject(Nothing)
会返回true
。更好的方法是使用If <variable-name> Is Nothing Then ...
。
完整的代码。 HTH
' Add reference to Microsoft Internet Controls (SHDocVw)
' Add reference to Microsoft HTML Object Library
Sub LetsAutomateIE()
Dim IE As SHDocVw.InternetExplorer
Dim barcode As String
Dim rowe As Integer
Dim document As HTMLDocument
Dim Element As HTMLDivElement
Dim result01 As HTMLListElement
Dim noResults As HTMLHeaderElement
Dim text As String
Dim pos As Integer
Dim url As String
rowe = 2
url = "https://www.amazon.co.uk/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords="
Set IE = New SHDocVw.InternetExplorer
While Not IsEmpty(Cells(rowe, 2))
barcode = Cells(rowe, "B").Value
pos = 0
text = ""
IE.Navigate url & barcode
While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
DoEvents
Wend
Set document = IE.document
Set result01 = document.getElementById("result_0")
If result01 Is Nothing Then
Set noResults = document.getElementById("noResultsTitle")
If Not noResults Is Nothing Then MsgBox noResults.outerText
GoTo Here
End If
text = document.getElementById("result_0").innerText
If InStr(text, "STEELBOOK") Or InStr(text, "Steelbook") Or InStr(text, "Steel book") <> 0 Then pos = 1
If pos <> 0 Then Cells(rowe, 4) = "Y" Else Cells(rowe, 4) = "N"
Here:
rowe = rowe + 1
Wend
IE.Quit
Set IE = Nothing
End Sub
我实际上只想查看第一个返回的标题 页面上的产品......
标题显示h2
元素li
,内容为result_0
。因此,可以将搜索范围限制为此li
元素并搜索第一个h2
元素。
' text = document.getElementById("result_0").innerText
Dim h2Elements As IHTMLElementCollection
Dim h2 As HTMLHeadElement
Set h2Elements = result01.getElementsByTagName("h2")
If h2Elements.Length > 0 Then
Set h2 = h2Elements.Item(0)
text = h2.innerText
Debug.Print text
Else
MsgBox "Text not found"
End If
输出:
RED 2 Blu-ray Steelbook UK Exclusive
The Hunger Games With Mockingjay Pendant
The Hunger Games
The Hunger Games
Avengers Assemble BD Steelbook
Avengers Assemble Bonus Disc BD Retail
答案 1 :(得分:0)
我遇到了document.getElementById("result_0")
抛出错误的问题。我的解决方法是测试元素是否在Document.Body.InnerHTML中。
如果您将DebugMode
设置为True,那么结果不佳的网页会保持打开状态以供进一步检查。
如果找不到,条形码将被标记为NA。
Option Explicit
Sub LetsAutomateIE()
Const DebugMode As Boolean = True
Dim barcode As String, text As String
Dim rowe As Integer
Dim doc As HTMLDocument, liResults As HTMLLIElement
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
rowe = 2
While Not IsEmpty(Cells(rowe, 2))
barcode = Cells(rowe, "B").Value
With ie
.Visible = False
.navigate2 "https://www.amazon.co.uk/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords=" & barcode
Do Until ie.readyState = 4
Loop
End With
Set doc = ie.document
If InStr(doc.body.innerHTML, "li id=""result_0""") Then
Set liResults = doc.getElementById("result_0")
text = liResults.innerText
Cells(rowe, 4) = IIf(InStr(text, "STEELBOOK") Or InStr(text, "Steelbook") Or InStr(text, "Steel book"), "Y", "N")
Else
Cells(rowe, 4) = "NA"
If DebugMode Then
ie.Visible = True
Set ie = CreateObject("InternetExplorer.Application")
End If
End If
rowe = rowe + 1
Wend
ie.Quit
Set ie = Nothing
End Sub