In this post, I demonstrate how to Scrape data from a Website and import it to a Google Sheet. I used JavaScript and Reg Expressions to accomplish the task.
How to Video:
Video Notes:
- Reg Expression Tool: https://regex101.com/
- Apps Script (Script Editor) is now located under tab ‘Extensions’ instead of ‘Tools’ on Google Sheets
- Check out another Scraping Website Post. Click Here
Code in Video:
function mainFunction()
{
// *************************************************************************
// CODE WITH CURT 2/14/2021
// The code below will eventually not bring back the desired results.
// Over time Web sites get Updated and Upgraded losing the original HTML and CSS tags that
// are searched in the RegExp statements.
// *************************************************************************
clearRecords();
var url = "https://www.premierleague.com/tables";
var str = UrlFetchApp.fetch(url).getContentText();
const mainRegex = /<td class=\"team\" scope=\"row\">([\s\S]*?)<\/td>/gi;
var results = str.match(mainRegex);
const teamNamePass1 = /<span class=\"long\">([\s\S]*?)<\/span>/gi;
const teamNamePass2 = /(?<=<span class=\"long\">).*?(?=<\/span>)/gi;
const teamLogoPass = /(?<=img class=\"badge-image badge-image--25\" src=\").*?(?=\")/gi;
for(var i = 0; i < 20; i++)
{
Logger.log('content: ' + results[i]);
var team1NameString = results[i].match(teamNamePass1);
Logger.log('content: ' + team1NameString[0]);
var team2NameString = team1NameString[0].match(teamNamePass2);
Logger.log('content: ' + team2NameString[0]);
var team1Logo = results[i].match(teamLogoPass);
Logger.log('content: ' + team1Logo[0]);
addRecord(i+1, team2NameString[0], team1Logo[0]);
}
}
function clearRecords()
{
var ss= SpreadsheetApp.getActiveSpreadsheet();
var tableSheet = ss.getSheetByName("TABLE");
tableSheet.getRange("A1:D20").clear();
var images = tableSheet.getImages();
for (var i = 0; i < images.length; i++)
{
var img = images[i];
img.remove();
}
}
function addRecord(count, team, logo) {
var ss= SpreadsheetApp.getActiveSpreadsheet();
var tableSheet = ss.getSheetByName("TABLE");
var currentRow = tableSheet.getLastRow();
var nextRow = currentRow + 1;
tableSheet.setRowHeight(nextRow, 30);
tableSheet.getRange(nextRow,1).setValue(count);
tableSheet.insertImage(logo, 2, nextRow);
tableSheet.getRange(nextRow,3).setValue(team);
tableSheet.getRange(nextRow,4).setValue(new Date());
}
Related Posts
How to Back Up Google Drive Files using Apps Script - In this post, I demonstrate how to back up Google Drive Files using Google Apps Script with Google Sheets.
How to Embed Charts on Google Sheets using Apps Script - In this post, I demonstrate How to Create Charts on Google Sheets using Google Apps Script.
How to Search Google Drive on Google Sheets using Apps Script - In this post, I demonstrate how to search Google Drive by File Name on Google Sheets using Apps Script
How to Create a Menu on Google Sheets using Apps Script - In this post, I demonstrate how to Create a Customer Menu on Google Sheets using Google Apps Script.
How to Get and Set Values on Google Sheets using Apps Script - In this post, I demonstrate how to Get and Set Values using Google Apps Script on Google Sheets.
How to Create Data Validation on Drop Down using Google Apps Script - In this post, I demonstrate How to Create Data Validation Drop-down List Using Google Apps Script on Google Sheets.
Create Stock Trend Analyzer on Google Sheets - In this video, I demonstrate how to analyze stock using the GOOGLEFINANACE Function on Google Sheets with Google Apps Script.
Scrape Amazon Website to Google Sheets - In this video, I demonstrate how to Scrape Amazon Website to Google Sheets using software called ScraperAPI.
Create Alert Popups on Google Sheets - In this post, I demonstrate how to create 3 different types of alert popups using Google Apps Script on Google Sheets.
Combine Multiple Spreadsheets into One Google Sheet - In this post, I demonstrate how to Combine Multiple Spreadsheets into one Sheet using Google Apps Script.
How to Get Next Open Row and Column using Google Apps Script - In this post, I demonstrate how to get the next open row and column using Google Apps Script on Google Sheets.
How to Get Map Time and Distance using Google Apps Script - In this post, I demonstrate how to get the Map Time and Distance between locations using Google Apps Script using the Map Class.
How to Create Dependent Dropdown on Google Sheets - In this post, I demonstrate how to Create a Dependent Dropdown list on Google Sheets using Google Apps Script.
How to Call Rest API on Google Sheets - In this post, I demonstrate how to call a Rest API using Google Apps Script and Displaying the data on a Google Sheet.
How to Create Google Doc from Apps Script - In this post, I demonstrate how to create a Google Doc using Google Apps Script and save it in Google Drive. In this example, I use the data from a Google Sheet.
How to Clear, Delete, Insert, and Replace Row using Google Apps Script - In this post, I demonstrate how to clear, delete, insert, and replace rows using Google Apps Script on Google Sheets.
Email Last Update on Google Sheet - In this post, I demonstrate how to email the last update made on a google sheet. This is a way to monitor any changes made on Google Sheets
How to Call JSON Web Service to Populate Google Sheet - In this post, I demonstrate how to Call a JSON Web Service and Populate a Google Sheet with a table of raw data.
Create Report Builder on Google Sheets using QUERY Function - In this post, I demonstrate how to use the QUERY function and Google Apps Script and build multiple report views from a list.
Copy Row from Sheet to Sheet using Apps Script on Google Sheets - In this post, I show how to copy a row of data from one sheet to another using Google Apps Script. This video also shows how to search a column on Google Sheets.