+ Reply to Thread
Results 1 to 4 of 4

Web Scrape "Name" from webpage based on URL in Column A

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    266

    Web Scrape "Name" from webpage based on URL in Column A

    I have a spreadsheet with various URLs column "A" and I am trying obtain a portion of the website for each link in Column B. Specifically, I am looking for the"Name" in website so I know which URL goes to which person.

    Attached is a sample of the worksheet I am working with. Column A has the URLs, and column B has the names. Is there any way to obtain the name automatically obtain the name for Column B rather than manually going to the URL? Thanks.


    NPI.xlsx

  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: Web Scrape "Name" from webpage based on URL in Column A

    Hello Brawnystaff,

    I have added two macros to the attached workbook which is the original you post. There is button to run the macros. It will list all the names as they appear on the web page.

    Here are the macro that have been added...

    Get the HTML Document from a URL
    ' Written: December 31, 2013
    ' Author:  Leith Ross
    ' Summary: Returns the HTML DOM from a given URL.
    
    Function GetHTMLdocument(ByVal URL As String) As Object
            
        Dim HTMLdoc     As Object
        Dim PageSrc     As String
          
          
          ' Retrieve the web page's HTML code (page source) from the server.
            With CreateObject("MSXML2.XMLHTTP")
                .Open "GET", URL, True
                .Send
            
                While .readyState <> 4: DoEvents: Wend
            
              ' Check for any connection errors.
                If .statusText <> "OK" Then
                    MsgBox "ERROR: " & .Status & " - " & .statusText, vbExclamation
                    Exit Function
                End If
            
                PageSrc = .ResponseText
            End With
                
          ' Create an empty HTML Document.
            Set HTMLdoc = CreateObject("htmlfile")
            
          ' Convert the Page Source into an HTML document.
            HTMLdoc.body.innerHTML = PageSrc
            
          ' Close the HTML file.
            HTMLdoc.Close
            
          ' Return the HTML Document Object.
            Set GetHTMLdocument = HTMLdoc
            
    End Function
    List the Provider Names
    ' Thread:  http://www.excelforum.com/excel-programming-vba-macros/980331-web-scrape-name-from-webpage-based-on-url-in-column-a.html
    ' Poster:  Brawnystaff
    ' Written: January 10, 2014
    ' Author:  Leith Ross
    
    Sub ListProviderNames()
    
        Dim Cell As Range
        Dim HTMLdoc As Object
        Dim ProviderName As String
        Dim Rng As Range
        Dim RngEnd As Range
        Dim oTable As Object
        Dim Wks As Worksheet
        
            
            Set Wks = Sheet1
            Set Rng = Wks.Range("A2")
            
              ' Exit if there are no URLs.
                Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
                If RngEnd.Row < Rng.Row Then Exit Sub
                
              ' Size the range of URLs.
                Set Rng = Rng.Resize(RngEnd.Row - Rng.Row + 1, 1)
                
              ' Clear any previous names.
                Rng.Offset(0, 1).ClearContents
                
              ' Get the provider name from each URL.
                For Each Cell In Rng
                    Set HTMLdoc = GetHTMLdocument(Cell.Value)
                    
                    On Error Resume Next
                    
                      ' Search all the tables for a cell labeled "Name:".
                        For Each oTable In HTMLdoc.getelementsbytagname("table")
                            ProviderName = oTable.Rows(0).Cells(1).ChildNodes(0).innerHTML
                            
                          ' No error if there is a match.
                            If Err = 0 Then
                                If Left(ProviderName, 5) = "Name:" Then
                                  ' Get the provider name.
                                    ProviderName = oTable.Rows(0).Cells(2).innerHTML
                                    Exit For
                                End If
                            End If
                            
                            Err.Clear
                            ProviderName = ""
                        Next oTable
                        
                    On Error GoTo 0
                    Cell.Offset(0, 1).Value = ProviderName
                Next Cell
                
          ' Listing is complete.
            MsgBox "All Names have been Listed."
            
    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
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    266

    Re: Web Scrape "Name" from webpage based on URL in Column A

    Outstanding!!! It worked great. Thanks so much...

  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: Web Scrape "Name" from webpage based on URL in Column A

    Hello Brawnystaff,

    You're welcome. Glad I could help out.

+ 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. [SOLVED] If there is any text in column "A$" on "sheet1" then move cell to column "A$" on "sheet2"
    By ckgeary in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-27-2013, 08:28 PM
  2. Replies: 2
    Last Post: 06-06-2013, 12:45 PM
  3. Replies: 3
    Last Post: 04-14-2013, 11:53 PM
  4. Replies: 2
    Last Post: 08-17-2012, 05:10 AM
  5. Insert a value in Column "C" based on the Total in Column "A"
    By Matthew_12345 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-12-2011, 09:08 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