I want to minimize this code. It takes a lot of time to do it. Is it? It is amazon data scraping code

9518423044

New Member
Joined
Aug 13, 2024
Messages
3
Office Version
  1. 2021
Platform
  1. Windows
VBA Code:
Sub ScrapeAmazonPrices()
    Dim ie As Object
    Dim html As Object
    Dim urls As Range
    Dim cell As Range
    Dim price As String
   
    ' Initialize Internet Explorer
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = False
   
    ' Set the range of URLs
    Set urls = ThisWorkbook.Sheets("Sheet1").Range("A1:A" & ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row)
   
    ' Loop through each URL
    For Each cell In urls
        If cell.Value <> "" Then
            ' Navigate to the URL
            ie.navigate cell.Value
            Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
           
            ' Get the HTML document
            Set html = ie.document
           
            ' Extract the price
            On Error Resume Next
            price = html.getElementsByClassName("a-price-whole")(0).innerText
            On Error GoTo 0
           
            ' Write the price in the next column
            cell.Offset(0, 1).Value = price
        End If
    Next cell
   
    ' Quit Internet Explorer
    ie.Quit
    Set ie = Nothing
    Set html = Nothing
   
    MsgBox "Scraping Completed!"
End Sub
 
Last edited by a moderator:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Welcome to the Board!

Loops are notoriously slow in VBA. However, we may be able to speed up the code a little by temporarily disabling things like calculations, events, and screen updating while the code is running. See if this helps any.

VBA Code:
Sub ScrapeAmazonPrices()
    Dim ie As Object
    Dim html As Object
    Dim urls As Range
    Dim cell As Range
    Dim price As String
   
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
   
    ' Initialize Internet Explorer
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = False
   
    ' Set the range of URLs
    Set urls = ThisWorkbook.Sheets("Sheet1").Range("A1:A" & ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row)
   
    ' Loop through each URL
    For Each cell In urls
        If cell.Value <> "" Then
            ' Navigate to the URL
            ie.navigate cell.Value
            Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
           
            ' Get the HTML document
            Set html = ie.document
           
            ' Extract the price
            On Error Resume Next
            price = html.getElementsByClassName("a-price-whole")(0).innerText
            On Error GoTo 0
           
            ' Write the price in the next column
            cell.Offset(0, 1).Value = price
        End If
    Next cell
   
    ' Quit Internet Explorer
    ie.Quit
    Set ie = Nothing
    Set html = Nothing
   
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.EnableEvents = True
   
    MsgBox "Scraping Completed!"
    
End Sub
 
Upvote 0
Welcome to the Board!

Loops are notoriously slow in VBA. However, we may be able to speed up the code a little by temporarily disabling things like calculations, events, and screen updating while the code is running. See if this helps any.

VBA Code:
Sub ScrapeAmazonPrices()
    Dim ie As Object
    Dim html As Object
    Dim urls As Range
    Dim cell As Range
    Dim price As String
  
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
  
    ' Initialize Internet Explorer
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = False
  
    ' Set the range of URLs
    Set urls = ThisWorkbook.Sheets("Sheet1").Range("A1:A" & ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row)
  
    ' Loop through each URL
    For Each cell In urls
        If cell.Value <> "" Then
            ' Navigate to the URL
            ie.navigate cell.Value
            Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
          
            ' Get the HTML document
            Set html = ie.document
          
            ' Extract the price
            On Error Resume Next
            price = html.getElementsByClassName("a-price-whole")(0).innerText
            On Error GoTo 0
          
            ' Write the price in the next column
            cell.Offset(0, 1).Value = price
        End If
    Next cell
  
    ' Quit Internet Explorer
    ie.Quit
    Set ie = Nothing
    Set html = Nothing
  
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.EnableEvents = True
  
    MsgBox "Scraping Completed!"
   
End Sub
its not working any free tool for amazon data scaping? suggest me ,
 
Upvote 0
What do you mean it is not working?
Do you mean it is still working, but just not going any faster?

I did not add anything to the code that should stop it from working, if it was working originally (which your original message seems to imply).
I just adding some temporary setting adjustments to halt screen updating, calculations, and event procedure calling while the body of the code is running.
 
Upvote 0
What do you mean it is not working?
Do you mean it is still working, but just not going any faster?

I did not add anything to the code that should stop it from working, if it was working originally (which your original message seems to imply).
I just adding some temporary setting adjustments to halt screen updating, calculations, and event procedure calling while the body of the code is running.





I understand what you said and you are absolutely correct.

I am looking for a good tool for data scraping.
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,173
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top