How to Do Web Scraping in Excel (2 Methods)

Method 1 – Using the “Get Data from Web” Feature

  • Go to the Data tab and select From Web under the Get & Transform Data section.

Opening Web Feature

  • Insert the URL of the website you want to scrape.

Inserting Website URL

  • Click OK and choose Anonymous for web access.
  • The website data will be linked to Excel. If there are multiple tables, select the one you want and click Transform Data.

Selecting Data Table from Website

  • In the Power Query Editor, remove unnecessary columns and make adjustments.
  • Load the table into a specific sheet using Close & Load To.

Extracting Data to Excel Sheet from Power Query Editor

  • Insert the cell reference where you want to extract the table to in the Import Data dialog box and click OK.

Choosing Reference for Extracted table

The data table will be imported to the desired cell of the sheet.

Table Extracted from Website

  • To update the table, select any cell and go to Data, select Refresh All and click on Connection Properties.

Refreshing Table to Update Any Change of Data in Website

  • The Query Properties window will appear. Check the Refresh every option and set a suitable time. Set the timer to 10 so that the data table will update every 10 minutes.

Setting up Timer to Refresh Data


Method 2 – VBA for Web Scraping

2.1 Scraping Data from Web as Table

  • Press Alt+F11 or select Developer and click on Visual Basic to open the VBA editor.

Opening VBA Window

  • Create a VBA Module and insert the provided code:

Inserting VBA Module

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.investing.com/equities/"
    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

VBA Code to Extract Data Table from Website

How Does the Code Work?

  • In this case, we utilized the Private Sub Procedure UseQueryTable() to extract tabular data from the website of interest.
  • A With Statement helps to organize the data on the Sheet5 sheet, which we titled Result (see the worksheet).
  • The Private Sub Procedure ClearSheet deletes previously used tables, making it easier to scrape new data from the URL.
  • The Public Sub Procedure (ExtractStockData) takes the commands from these Private Sub Procedures to scrape the tabular data from the website.

You can use the code; you just need to change the website link if needed.

  • Press Alt + F8 to run the macro and scrape the table.

Running Macro to Scrape Data Table

The table will be scraped to the sheet named Result.

Data Table Extracted by VBA

  • Format the data table for better presentation.

Formatted Data Table


2.2 Scraping Entire Page Data from Web 

  • Press Alt+F11 or select Developer and click on Visual Basic to open the VBA editor.
  • Create a VBA Module and insert the provided code.
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.investing.com/equities/"
    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

VBA Code to Extract Entire Page

How Does the Code Work?

  • We utilized the Private Sub Procedure UseQueryTable() to retrieve tabular data from the website of our choice.
  • A With Statement helps in organizing the data on the Sheet6 page, which we labeled Entire Page (see the worksheet).
  • The Private Sub Procedure ClearSheet deletes previously used tables, allowing new data to be scraped from the URL.
  • The Public Sub Procedure (ScrapeFullPage) scrapes the full webpage from the website using the commands from these Private Sub Procedures.
  • The code scrapes data from a specified URL and organizes it on a sheet named Entire Page (Sheet6).
  • Customize the URL as needed.
  • Press Alt + F8 to run the macro and scrape the entire webpage, including hyperlinks.
  • After running the code, you’ll find all the data (including links) in your Excel sheet.

The following picture shows the first parts of the data from the website.

Part of Extracted Entire Page in Excel Sheet

And here is the image after a bit of scrolling, showing the data table we scraped earlier using the Macro named ExtractStockData.

Another Part of Scraped Data from Entire Page


Things to Remember

  • Remember that VBA code may need adjustments for different webpage layouts.
  • In the 1st method, we can scrape multiple objects from a webpage at a time.

Download Practice Workbook

You can download the practice workbook from here:


Frequently Asked Questions

1. How does web scraping in Excel work?

  • Web scraping in Excel typically involves using Excel’s built-in web query functionality or external tools and libraries.
  • Specify the URLs you want to scrape and define the data elements to extract.
  • Configure the scraping process according to your requirements.

2. Are there any limitations or legal considerations when web scraping in Excel?

  • Some websites may prohibit scraping via terms of service or robots.txt files.
  • Comply with applicable laws and regulations.
  • Be aware of rate limits and anti-scraping measures.

Web Scraping in Excel: Knowledge Hub


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


Get FREE Advanced Excel Exercises with Solutions!
Meraz Al Nahian
Meraz Al Nahian

Md. Meraz Al Nahian has worked with the ExcelDemy project for over 1.5 years. He wrote 140+ articles for ExcelDemy. He also solved a lot of user problems and worked on dashboards. He is interested in data analysis, advanced Excel, statistics, and dashboards. He also likes to explore various Excel and VBA applications. He completed his graduation in Electrical & Electronic Engineering from Bangladesh University of Engineering & Technology (BUET). He enjoys exploring Excel-related features to gain efficiency... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo