+ Reply to Thread
Results 1 to 5 of 5

How to extract a specific data from a website using macros or vba

Hybrid View

pari9485 How to extract a specific... 10-07-2013, 02:33 PM
Leith Ross Re: How to extract a specific... 10-07-2013, 10:56 PM
pari9485 Re: How to extract a specific... 10-08-2013, 01:13 AM
Leith Ross Re: How to extract a specific... 10-08-2013, 02:45 PM
pari9485 Re: How to extract a specific... 10-08-2013, 10:42 PM
  1. #1
    Registered User
    Join Date
    03-06-2013
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    17

    How to extract a specific data from a website using macros or vba

    I have to note down the no. of reviews from a website page and paste it into excel, Eg: http://www.junglee.com/dp/B00D8WDF22 have 15 reviews on the page and I have to look in the website and write the no of reviews one by one. If the page does not have review then I leave it blank.

    Can anyone help me to automate this? It will be very helpful as I have to do this for 1000 products daily


    I have attached my excel sheet.
    Attached Files Attached Files
    Last edited by pari9485; 10-07-2013 at 02:39 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to extract a specific data from a website using macros or vba

    Hello pari9485,

    The following macros have been added to the attached workbook. There are in Module1 of the VBA project. A button has been added to the sheet to run the macro.

    Module1 Macros
    Function GetReviewsCount(ByVal URL As String) As Variant
          
        Dim htmlDoc     As Object
        Dim htmlSpan    As Object
        Dim PageSrc     As String
        
            PageSrc = ""
            
            With CreateObject("MSXML2.XMLHTTP")
                .Open "GET", URL, True
                .Send
                                    
                While .readyState <> 4: DoEvents: Wend
                                
              ' Check for any connection errors. Put the error info in cells A4:B4.
                If .statusText <> "OK" Then
                    MsgBox "ERROR" & .Status & " - " & .statusText, vbExclamation
                    Exit Function
                End If
                                
                PageSrc = .ResponseText
            End With
    
          ' Create an empty HTML Document and load it with the PageSource.
            Set htmlDoc = CreateObject("htmlfile")
            htmlDoc.Open URL:="text/html", Replace:=False
                
          ' NOTE: This will check if cookies are enabled and prompt you if they aren't.
            htmlDoc.write PageSrc
            
                For Each htmlSpan In htmlDoc.getElementsByTagName("span")
                    If htmlSpan.ClassName = "partner-reviews-count" Then
                        If htmlSpan.NodeType = 1 Then
                            GetReviewsCount = Val(htmlSpan.innerHTML)
                            Exit Function
                        End If
                    End If
                Next htmlSpan
    
            GetReviewsCount = "No Review"
            
    End Function
    
    Sub Macro1()
    
        Dim Cell    As Range
        Dim EndRow  As Long
        Dim Rng     As Range
        Dim Wks     As Worksheet
        
            Set Wks = ActiveSheet
            Set Rng = Wks.Range("A2")
            
                EndRow = Wks.Cells(Rows.Count, "A").End(xlUp).Row
                If EndRow < Rng.Row Then Exit Sub Else Set Rng = Rng.Resize(EndRow - Rng.Row + 1, 1)
                
                For Each Cell In Rng
                    Cell.Offset(0, 1) = GetReviewsCount(Cell)
                Next Cell
            
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    03-06-2013
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: How to extract a specific data from a website using macros or vba

    Thanks a lot, You are really a genius . It is working and is extremely helpful. Just one thing , while the macro runs , internet explorer pages starts opening, How Can I stop it to open?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to extract a specific data from a website using macros or vba

    Hello pari9485,

    After many hours of reading and testing, I have no idea why this is happening. I have saved the page source as a file and edited it several times thinking it could be something in the HTML or JavaScript code. Each edit failed to prevent the Internet Explorer popup. I am running Internet Explorer 10 and according to everything I have read this message should not occur. This was a problem in IE 7 and Microsoft fixed starting with version 8.

    I can use this method method to open many other sites with no problem. My best guess is the problem is somehow server related. At this time, I have no workaround to the problem. Perhaps another member has one to offer.

  5. #5
    Registered User
    Join Date
    03-06-2013
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: How to extract a specific data from a website using macros or vba

    Its OK . Thanks a lot.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Extract filtered data from a website
    By jakg88 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-30-2013, 01:20 AM
  2. Extract data from website to an excel
    By vgothe in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-27-2012, 10:51 AM
  3. Website Data Extract Problem
    By KayG in forum Excel General
    Replies: 1
    Last Post: 08-03-2012, 10:24 AM
  4. extract data from website to excel
    By asdzxc in forum Excel General
    Replies: 2
    Last Post: 05-11-2012, 04:20 AM
  5. Excel 2007 : Extract Data from Website
    By Testify in forum Excel General
    Replies: 1
    Last Post: 05-24-2011, 08:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1