1

I am want to scrap from amazon some fields.

Atm I am using a link and my vba script returns me name and price.

For example:

I put the link into column A and get the other fields in the respective columns, 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

However, I would also like to have the product description.

Here is my current code:

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

I am trying to get the product description by using productDesc = Docx.getElementsByClassName("productDescriptionWrapper")(0).innerText.

However, I get an error.

Object variable or with block variable not set.

Any suggestion why my statement does not work?

I appreciate your replies!

2
  • You are declaring variables, but they are all variants, since you don't assign a data type. That is not good practice. You may as well not declare them in the first place. You are wasting resources. Commented Jul 13, 2015 at 8:47
  • Which Internet Explorer version is installed in this system? IE lower than 9.0 has no method document.getElementsByClassName. Commented Jul 13, 2015 at 9:37

1 Answer 1

1

I'm pretty sure your problem is being caused by attempting to access the document before it's completely loaded. You're just checking ie.ReadyState.

This is my understanding of the timeline for loading a page with an IE control.

  1. Browser connects to page: ie.ReadyState = READYSTATE_COMPLETE. At this point, you can access ie.document without causing an error, but the document has only started loading.
  2. Document fully loaded: ie.document.readyState = "complete" (note that frames may still be loading and AJAX processing may still be occurring.)

So you really need to check for two events.

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

edit: after actually looking at the page you're trying to scrape, it looks like the reason it's failing is because the content you're trying to get at is in an iframe. You need to go through the iframe before you can get to the content.

ie.document.window.frames("product-description-iframe").contentWindow.document.getElementsByClassName("productDescriptionWrapper").innerText
Sign up to request clarification or add additional context in comments.

2 Comments

Thx for your answer! I tried to save the product description in a variable with productDesc = Docx.Window.frames("product-description-iframe").contentWindow.document.getElementsByClassName("productDescriptionWrapper").innerText , but I get Object does not support this property or method. Any recommendation how to initialize my variable productdesc to save the output to the cell?
Probably has to do with how I'm accessing the iframe. Sorry I'm not going to have time today to debug this myself. If you spend some time with the VBA watch window and immediate pane you should be able to figure it out. The thing you need to figure out is how to get to the iframe document object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.