使用getElementsByClassName时出错“对象variables或块variables未设置”

我想从亚马逊一些领域报废。

Atm我正在使用一个链接,我的vba脚本返回我的名字和价格。

例如:

我把链接放到列A中,并获得其他字段在各自的列,f.ex .: http://www.amazon.com/GMC-Denali-Black-22-5-Inch-Medium/dp/B00FNVBS5C/ref=sr_1_1?s=outdoor-recreation&ie=UTF8&qid=1436768082&sr=1-1&keywords=bicycle

不过,我也想要有product description

这是我目前的代码:

 Sub ScrapeAmz() Dim Ie As New InternetExplorer Dim WebURL Dim Docx As HTMLDocument Dim productDesc Dim productTitle Dim price Dim RcdNum 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 'productDesc = Docx.getElementsByClassName("productDescriptionWrapper")(0).innerText price = Docx.getElementById("priceblock_ourprice").innerText ThisWorkbook.Worksheets(1).Range("B" & RcdNum) = productTitle 'ThisWorkbook.Worksheets(1).Range("C" & RcdNum) = productDesc ThisWorkbook.Worksheets(1).Range("D" & RcdNum) = price Next End Sub 

我试图通过使用productDesc = Docx.getElementsByClassName("productDescriptionWrapper")(0).innerText获取产品说明。

但是,我得到一个错误。

 Object variable or with block variable not set. 

任何build议,为什么我的声明不起作用?

我很感激你的回复!

我很确定你的问题是由于在完全加载之前尝试访问文档而导致的。 你只是检查ie.ReadyState。

这是我对用IE控件加载页面的时间表的理解。

  1. 浏览器连接到页面: ie.ReadyState = READYSTATE_COMPLETE 。 此时,您可以访问ie.document而不会导致错误,但文档只是开始加载。
  2. 文件完全加载: ie.document.readyState = "complete" (请注意,框架可能仍然在加载和AJAX处理可能仍在发生。

所以你真的需要检查两个事件。

 Do If ie.ReadyState = READYSTATE_COMPLETE Then If ie.document.readyState = "complete" Then Exit Do End If Application.Wait DateAdd("s", 1, Now) Loop 

编辑:在实际看着你想要抓取的页面之后,看起来它失败的原因是因为你试图获取的内容在iframe中。 您需要先浏览iframe,然后才能看到内容。

 ie.document.window.frames("product-description-iframe").contentWindow.document.getElementsByClassName("productDescriptionWrapper").innerText