+ Reply to Thread
Results 1 to 3 of 3

EXCEL VBA scraping webpage

Hybrid View

  1. #1
    Registered User
    Join Date
    12-08-2019
    Location
    brazil
    MS-Off Ver
    365
    Posts
    1

    EXCEL VBA scraping webpage

    having a few issues getting an excel vba to scrape a webpage.

    I require a vba to do the following

    1. visit the url on the excel sheet - There is a whole list of 100 URL's.
    2. extract 3 data sets
    3. populate 3 columns in the excel sheet
    4. Loop to next url in list.

    I have managed to extract the data i need and get it into the immediate view window in but I am struggling to set the value of the URL from the cell and loop down the list.

    I wish i could share the website but there is a login and password access required.

    i have put +++++ where the url should be.

    any pointers or help i will be grateful for


    Sub Browsetosite()
    
        Dim IE As New SHDocVw.InternetExplorer
        Dim HTMLDoc As MSHTML.HTMLDocument
        Dim HTMLInput As MSHTML.IHTMLElement
        Dim HTMLTrs As MSHTML.IHTMLElementCollection
        Dim HTMLTr As MSHTML.IHTMLElement
        Dim rows As Long
        
    
        IE.Visible = True
        
        IE.Navigate "+++++", False
        
        
        Do While IE.ReadyState <> READYSTATE_COMPLETE
        Loop
        
        Set HTMLDoc = IE.Document
        
      
        Set HTMLTrs = HTMLDoc.getElementsByTagName("td")
            
        For Each HTMLTr In HTMLTrs
    
    
       ' Debug.Print HTMLTr.getAttribute("id"), ",", HTMLTr.innerText, HTMLTr.innerHTML
                         
        'code below find eb number title and code and assigns it to row E1, F1 and G1
        If HTMLTr.getAttribute("id") = "ctl00_bodyContents_td_displayPageContainer_props_prefix_0" Then
        Range("E1").Value = (HTMLTr.innerText)
        End If
        
        If HTMLTr.getAttribute("id") = "ctl00_bodyContents_td_displayPageContainer_props_title_0" Then
        Range("F1").Value = (HTMLTr.innerText)
        End If
        
        If HTMLTr.getAttribute("id") = "ctl00_bodyContents_td_displayPageContainer_props_Review_and_Acceptance_Status_2" Then
        Range("G1").Value = (HTMLTr.innerText)
        End If
        
        'Debug.Print HTMLTr.innerText
        
        Next HTMLTr
    
    End Sub
    Last edited by Pepe Le Mokko; 12-09-2019 at 03:38 AM.

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2504
    Posts
    13,621

    Re: EXCEL VBA scraping webpage

    Administrative Note:

    We would very much like to help you with your query, however you need to include code tags around your code. (not "quote" tags as you did)

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    I did it for you this time

  3. #3
    Valued Forum Contributor
    Join Date
    12-01-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2021
    Posts
    979

    Re: EXCEL VBA scraping webpage

    lets say your URL list in column A starting in A2
    try

    Sub Browsetosite()
    
        Dim IE As New SHDocVw.InternetExplorer
        Dim HTMLDoc As MSHTML.HTMLDocument
        Dim HTMLInput As MSHTML.IHTMLElement
        Dim HTMLTrs As MSHTML.IHTMLElementCollection
        Dim HTMLTr As MSHTML.IHTMLElement
        Dim rows As Long
        Dim x As Integer
        
        NumRows = Range("A2", Range("A2").End(xldown)).Rows.Count
        Range("A2").Select
    
        For x = 1 To NumRows
    
        IE.Visible = True
        
        IE.Navigate Activecell.Value, False
        
        
        Do While IE.ReadyState <> READYSTATE_COMPLETE
        Loop
        
        Set HTMLDoc = IE.Document
        
      
        Set HTMLTrs = HTMLDoc.getElementsByTagName("td")
            
        For Each HTMLTr In HTMLTrs
    
    
       ' Debug.Print HTMLTr.getAttribute("id"), ",", HTMLTr.innerText, HTMLTr.innerHTML
                         
        'code below find eb number title and code and assigns it to row E1, F1 and G1
        If HTMLTr.getAttribute("id") = "ctl00_bodyContents_td_displayPageContainer_props_prefix_0" Then
        Range("E1").Value = (HTMLTr.innerText)
        End If
        
        If HTMLTr.getAttribute("id") = "ctl00_bodyContents_td_displayPageContainer_props_title_0" Then
        Range("F1").Value = (HTMLTr.innerText)
        End If
        
        If HTMLTr.getAttribute("id") = "ctl00_bodyContents_td_displayPageContainer_props_Review_and_Acceptance_Status_2" Then
        Range("G1").Value = (HTMLTr.innerText)
        End If
        
        'Debug.Print HTMLTr.innerText
        
        Next HTMLTr
        ActiveCell.Offset(1, 0).Select
        Next
    End Sub

+ 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. Scraping Java Webpage into Excel 2013
    By free008 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-11-2023, 10:04 AM
  2. Trouble Scraping Webpage with Password
    By Josephrandall in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-27-2019, 06:16 AM
  3. [SOLVED] scraping webpage data modification
    By khushboo# in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-13-2017, 11:08 AM
  4. [SOLVED] Help scraping data from webpage into a sheet based on date
    By ozbad5243 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 11-23-2016, 06:43 PM
  5. [SOLVED] Webpage scraping into Excel - Run-time error '438': Object doesn't support this property..
    By kaseyleigh in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-29-2014, 08:15 AM
  6. Scraping website into excel
    By eodsolo in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-12-2014, 06:25 AM
  7. scraping a webpage for flood info
    By yarrick22 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-13-2014, 02:35 AM

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