How to Scrape Data from a Website into Excel (2 Easy Methods)

To illustrate our methods, we’ll use a table on a webpage (see the screenshot below) that we want to export into Excel for further analysis.

Sample data to show how to scrape data from a website into Excel

Method 1 – Using the Get Data From Web Feature

Steps:

  • Open the Excel file into which you want to extract data from a website. Here, “Scraping Data from Website.xlsx”.
  • Go to the Data tab.
  • Click on From Web in the Get & Transform Data group.

Utilizing Get Data From Web Feature for Scraping Data from a Website

The From Web pop-up window opens,

  • Enter the URL from where you want to scrape data.
  • Click OK.

Utilizing Get Data From Web Feature for Scraping Data from a Website

A Navigator window will open.

Utilizing Get Data From Web Feature for Scraping Data from a Website

On the left panel are a list of options; on the right side, tabs for Table View and Web View. If we click on the Web View tab, we will be able to see the web version of this webpage.

The best thing about this feature is that it can automatically detect any table or document in the webpage. If we click on any of the listed tables, the table will be shown in the preview window.

  • Now, if you view the table in Table View, you will see how the data will look in the Excel sheet.

Utilizing Get Data From Web Feature for Scraping Data from a Website

Before loading a table, we can make some transformations to clean it up and make it suitable for our use.

  • Click on Transform Data.

A Power Query Editor window will open like this:

Utilizing Get Data From Web Feature for Scraping Data from a Website

  • After doing the necessary editing, press Close & Load. 

The table will be loaded into the Excel sheet.

After some modifications and changes in formatting, the table looked like this:

Utilizing Get Data From Web Feature for Scraping Data from a Website

 

2. Using a VBA Macro

Here, we will not only scrape a data table from a webpage but also an entire webpage. The screenshot of the webpage is below. This website contains information on stock prices of some companies.

Application of VBA Macro to Scrape Data from a Website

Steps:

  • Press Alt+F11 to open the VBA editor.

  • Go to Insert > Module.

Application of VBA Macro to Scrape Data from a Website

A blank module will open up like this:

  • Copy and paste the following code into the editor:
Option Explicit
Public Sub ExtractStockData()
    Call ClearSheet
    Call UseQueryTable
End Sub
Private Sub ClearSheet()
    Dim aA_table As QueryTable
    For Each aA_table In Sheet5.QueryTables
        aA_table.Delete
    Next aA_table
    Sheet5.Cells.Clear
End Sub
Private Sub UseQueryTable()
    Dim aA_URL As String
    aA_URL = "https://www.cse.com.bd/market/current_price"
    Dim aA_table As QueryTable
    Set aA_table = Sheet5.QueryTables.Add("URL;" & aA_URL, Sheet5.Range("A1"))
    With aA_table
        .WebSelectionType = xlSpecifiedTables
        .WebTables = "1"
        .WebFormatting = xlWebFormattingNone
        .Refresh
    End With
End Sub

How Does the Code Work?

  • We used Private Sub Procedure UseQueryTable() to identify tabular data from the website.
  • A With Statement helps to arrange those data in the Sheet5 sheet which we named as Result (see in the workbook).
  • The Private Sub Procedure ClearSheet deletes previously used tables and thus helps to scrape fresh data from the URL.
  • Finally, the Public Sub Procedure (ExtractStockData) uses the commands from these Private Sub Procedures to scrape the tabular data from the website.
  • Run this code by clicking the F5 button.

The table will be inserted into Sheet 5.

Application of VBA Macro to Scrape Data from a Website

After some formatting, the table looks like this:

Application of VBA Macro to Scrape Data from a Website

  • To scrape the entire webpage, open a new module and paste the code below into it:
Option Explicit
Public Sub ScrapeFullPage()
    Call ClearSheet
    Call UseQueryTable
End Sub
Private Sub ClearSheet()
    Dim aA_table As QueryTable
    For Each aA_table In Sheet6.QueryTables
        aA_table.Delete
    Next aA_table
    Sheet6.Cells.Clear
End Sub
Private Sub UseQueryTable()
    Dim aA_URL As String
    aA_URL = "https://www.cse.com.bd/market/current_price"
    Dim aA_table As QueryTable
    Set aA_table = Sheet6.QueryTables.Add("URL;" & aA_URL, Sheet6.Range("A1"))
    With aA_table
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingAll
        .Refresh
    End With
End Sub

How Does the Code Work?

  • We used Private Sub Procedure UseQueryTable() to identify tabular data from the website.
  • A With Statement helps to arrange those data in the Sheet6 sheet which we named as Entire Page (see in the workbook).
  • The Private Sub Procedure ClearSheet deletes previously used tables and thus helps to scrape fresh data from the URL.
  • Finally, the Public Sub Procedure (ScrapeFullPage) uses the commands from these Private Sub Procedures to scrape the entire webpage from the website.

If you run this code by pressing F5, the entire webpage will be extracted into Sheet 6 named Entire Page.

If you scroll down, you will see the table that we scraped.

Application of VBA Macro to Scrape Data from a Website

 

Things to Remember

  • The VBA code provided is not universally applicable to every webpage. The code will need to be modified each time according to the webpage formatting. Hence, this method can be time-consuming and cumbersome.
  • In Method 1, we can scrape multiple objects from a webpage at a time.

Download Practice Workbook


Related Articles


<< Go Back to Web Scraping in Excel | Importing Data in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Aniruddah Alam
Aniruddah Alam

Md. Aniruddah Alam, a graduate of Bangladesh University of Engineering & Technology, Dhaka, holds a BSc.Engg in Naval Architecture & Marine Engineering. Joining Softeko in August 2022 as an Excel and VBA content developer, he has authored 90+ articles, covering fundamental to advanced Excel topics. He actively develops VBA codes for Excel automation and offers solutions in the Exceldemy Forum. Beyond work, he finds leisure in reading books, and taking relaxing walks, showcasing well-rounded blend of professional and... Read Full Bio

2 Comments
  1. How to scrape data when website requires authentication❓

    I do have a VALID user name and password; not trying to bypass security❗

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo