Build your own Reddit Scraper with Google Apps Script

Published in: Google Apps Script

Reddit Scraper is a Google Script that pulls all posts from any Reddit (subreddit) and saves the information in a Google sheet. The script extracts the post’s title, description, permalink and the posting date but can be easily extended to including user comments and thumbnail images as well.

The script runs through a background trigger every 5 minutes (configurable) and the trigger is automatically deleted once all the posts have been processed.

/* Reddit Scraper written by Amit Agarwal */
/* January 9, 2013 */

/* Replace LifeProTips with the Subreddit Name */
var REDDIT = "LifeProTips";

function run() {


  /* Fetch Reddit posts every 5 minutes to avoid hitting
     the reddit and Google Script quotas */

function scrapReddit() {

  // Process 20 Reddit posts in a batch
  var url = ""
            + REDDIT + "/new.xml?limit=20" + getLastID_();

  // Reddit API returns the results in XML format
  var response = UrlFetchApp.fetch(url);
  var doc = XmlService.parse(response.getContentText());
  var entries = doc.getRootElement()

  var data = new Array();

  for (var i=0; i<entries.length; i++) {

    /* Extract post date, title, description and link from Reddit */

    var date = entries[i].getChild('pubDate').getText();
    var title = entries[i].getChild('title').getText();
    var desc = entries[i].getChild('description').getText();
    var link = entries[i].getChild('link').getText();

    data[i] = new Array(date, title, desc, link);

  if (data.length == 0) {
    /* There's no data so stop the background trigger */
  } else {

/* Write the scrapped data in a batch to the
   Google Spreadsheet since this is more efficient */
function writeData_(data) {

  if (data.length === 0) {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var row = sheet.getLastRow();
  var col = sheet.getLastColumn();

  var range = sheet.getRange(row+1, 1, data.length, 4);
  try {
  } catch (e) {

/* Use the ID of the last processed post from Reddit as token */
function getLastID_() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var row = sheet.getLastRow();
  var col = sheet.getLastColumn();

  var url = sheet.getRange(row, col).getValue().toString();
  var pattern = /.*comments\/([^\/]*).*/;
  var id = url.match(pattern);

  return id ? "&after=t3_" + id[1] : "";


/* Posts Extracted, Delete the Triggers */
function deleteTriggers_() {
  var triggers = ScriptApp.getProjectTriggers();
  for (var i=0; i<triggers.length; i++) {
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube