即使属性在页面上,也无法删除属性

时间:2015-11-03 06:35:51

标签: excel excel-vba web-scraping excel-2010 spreadsheet vba

我正在尝试将一些数据放入Excel表格中。例如,当您将此链接放入"unzip SRCPath -d DestPath "

A2

发生以下错误:

enter image description here

以下是代码:

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

任何建议,我做错了什么?

更新

是的,我调试了我的代码。请参阅以下图片以获取概述:

enter image description here

我的问题是, 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 var imagePath或甚至进入此empty对我来说很奇怪。

1 个答案:

答案 0 :(得分:1)

尝试替换它:

    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