无法抓取属性,即使它在页面上
Attribute cannot be scraped, even though it's on the page
我正在尝试将一些数据输入我的 Excel sheet。例如,当您将此 link 放入 A2
时:
http://www.amazon.com/Faulkner-WL-2015A-6-Speed-Folding-Pedal/dp/B00S73PQ2E/ref=sr_1_26?s=outdoor-recreation&ie=UTF8&qid=1446493717&sr=1-26&keywords=folding+bike
出现以下错误:
代码如下:
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub ScrapeAmz()
Dim Ie As New InternetExplorer
Dim WebURL
Dim Docx As HTMLDocument
Dim productDesc
Dim productTitle
Dim price
Dim imagePath
Dim RcdNum
Dim imgObj
Dim featureBullets
Dim reviews
Ie.Visible = False
For RcdNum = 2 To ThisWorkbook.Worksheets(1).Range("A65536").End(xlUp).Row
WebURL = ThisWorkbook.Worksheets(1).Range("A" & RcdNum)
Ie.Navigate2 WebURL
Do Until Ie.readyState = READYSTATE_COMPLETE
DoEvents
Loop
Set Docx = Ie.document
productTitle = Docx.getElementById("productTitle").innerText
'####### Image
'Set imgObj = Docx.getElementsByTagName("img")
'imagePath = imgObj(1).getAttribute("src")
Dim el4 As MSHTML.IHTMLElement
On Error Resume Next
Set el4 = Docx.getElementById("landingImage").getAttribute("src")
On Error GoTo 0
If Not el4 Is Nothing Then
imagePath = ""
Else
imagePath = Docx.getElementById("landingImage").getAttribute("src")
End If
'print to workbook
ThisWorkbook.Worksheets(1).Range("B" & RcdNum) = productTitle
'ThisWorkbook.Worksheets(1).Range("C" & RcdNum) = productDesc
ThisWorkbook.Worksheets(1).Range("E" & RcdNum) = imagePath
Sleep (5000)
Next
End Sub
我在以下行收到错误:
Else
imagePath = Docx.getElementById("landingImage").getAttribute("src") ' here I get the error
End If
这部分的目的是,如果没有可用的属性,程序应该输出一个空字符串。
Dim el4 As MSHTML.IHTMLElement
On Error Resume Next
Set el4 = Docx.getElementById("landingImage").getAttribute("src")
On Error GoTo 0
If Not el4 Is Nothing Then
imagePath = ""
Else
imagePath = Docx.getElementById("landingImage").getAttribute("src")
End If
任何建议,我做错了什么?
更新
是的,我调试了我的代码。请参阅下图了解概览:
我的问题是 imagePath
var 是 empty
甚至进入这个 else-clause
.
看起来很奇怪
尝试替换这个:
If Not el4 Is Nothing Then
imagePath = ""
Else
imagePath = Docx.getElementById("landingImage").getAttribute("src")
End If
有了这个:
If el4 Is Nothing Then
imagePath = ""
Else
imagePath = Docx.getElementById("landingImage").getAttribute("src")
End If
我正在尝试将一些数据输入我的 Excel sheet。例如,当您将此 link 放入 A2
时:
http://www.amazon.com/Faulkner-WL-2015A-6-Speed-Folding-Pedal/dp/B00S73PQ2E/ref=sr_1_26?s=outdoor-recreation&ie=UTF8&qid=1446493717&sr=1-26&keywords=folding+bike
出现以下错误:
代码如下:
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub ScrapeAmz()
Dim Ie As New InternetExplorer
Dim WebURL
Dim Docx As HTMLDocument
Dim productDesc
Dim productTitle
Dim price
Dim imagePath
Dim RcdNum
Dim imgObj
Dim featureBullets
Dim reviews
Ie.Visible = False
For RcdNum = 2 To ThisWorkbook.Worksheets(1).Range("A65536").End(xlUp).Row
WebURL = ThisWorkbook.Worksheets(1).Range("A" & RcdNum)
Ie.Navigate2 WebURL
Do Until Ie.readyState = READYSTATE_COMPLETE
DoEvents
Loop
Set Docx = Ie.document
productTitle = Docx.getElementById("productTitle").innerText
'####### Image
'Set imgObj = Docx.getElementsByTagName("img")
'imagePath = imgObj(1).getAttribute("src")
Dim el4 As MSHTML.IHTMLElement
On Error Resume Next
Set el4 = Docx.getElementById("landingImage").getAttribute("src")
On Error GoTo 0
If Not el4 Is Nothing Then
imagePath = ""
Else
imagePath = Docx.getElementById("landingImage").getAttribute("src")
End If
'print to workbook
ThisWorkbook.Worksheets(1).Range("B" & RcdNum) = productTitle
'ThisWorkbook.Worksheets(1).Range("C" & RcdNum) = productDesc
ThisWorkbook.Worksheets(1).Range("E" & RcdNum) = imagePath
Sleep (5000)
Next
End Sub
我在以下行收到错误:
Else
imagePath = Docx.getElementById("landingImage").getAttribute("src") ' here I get the error
End If
这部分的目的是,如果没有可用的属性,程序应该输出一个空字符串。
Dim el4 As MSHTML.IHTMLElement
On Error Resume Next
Set el4 = Docx.getElementById("landingImage").getAttribute("src")
On Error GoTo 0
If Not el4 Is Nothing Then
imagePath = ""
Else
imagePath = Docx.getElementById("landingImage").getAttribute("src")
End If
任何建议,我做错了什么?
更新
是的,我调试了我的代码。请参阅下图了解概览:
我的问题是 imagePath
var 是 empty
甚至进入这个 else-clause
.
尝试替换这个:
If Not el4 Is Nothing Then
imagePath = ""
Else
imagePath = Docx.getElementById("landingImage").getAttribute("src")
End If
有了这个:
If el4 Is Nothing Then
imagePath = ""
Else
imagePath = Docx.getElementById("landingImage").getAttribute("src")
End If