How to Save Scraped Data Directly to Google Sheets

SwiftProxy
By - Emily Chan
2025-07-07 14:57:53

How to Save Scraped Data Directly to Google Sheets

Scraping data is powerful. But what's even better is automating where that data lands. If you're manually copying and pasting scraped info into spreadsheets, you're stuck in the past. It's time to streamline your workflow.
This guide walks you through saving scraped web data—specifically book details like titles, prices, and availability—directly into Google Sheets using Python. You'll learn how to scrape, organize, and store your data with minimal hassle.

What You'll Need to Start

Before diving in, make sure you have:

A Google Cloud account set up with Google Sheets API enabled.

A service account with Editor permissions and its JSON credentials file.

Python installed, along with these libraries: requests, beautifulsoup4, and gspread.

Use this command to install the libraries if you haven't already:

pip install requests beautifulsoup4 gspread

Set Up Google Sheets API Access

To interact with Google Sheets programmatically, you’ll need to:

Create a Google Cloud Project: Head to Google Cloud Console and spin up a new project.

Enable the Google Sheets API: Find it in the APIs library and enable it for your project.

Create a Service Account: Under IAM & Admin, create a service account with the Editor role.

Download Credentials: Grab the JSON key file for this service account and store it safely. You’ll need its file path soon.

If you get stuck, Google has solid documentation and plenty of tutorials walking through these steps.

Prepare Your Google Sheet

Create a new Google Sheet—let's call it Book Data.

Now, share this sheet with your service account email (found in your JSON file under client_email). Make sure to grant Editor permissions so your script can write data.

Alternatively, you could make the sheet public, but sharing with the service account is more secure.

Scrape Book Data with Python

We'll scrape data from the Books to Scrape demo site—a perfect sandbox for practicing.
We want three key pieces of info from each book on the first page:

Title

Price

Availability

Here's a Python function that does just that:

from typing import List
import requests
from bs4 import BeautifulSoup

def extract_book_data() -> List[dict]:
    url = "http://books.toscrape.com/"
    response = requests.get(url)
    book_data = []

    if response.status_code == 200:
        soup = BeautifulSoup(response.content, 'html.parser')
        books = soup.find_all('li', class_='col-xs-6 col-sm-4 col-md-3 col-lg-3')

        for book in books:
            title = book.find('h3').find('a')['title']
            price = book.find('p', class_='price_color').text
            availability = book.find('p', class_='instock availability').text.strip()

            book_data.append({
                'title': title,
                'price': price,
                'availability': availability
            })
    else:
        print(f"Failed to retrieve page: Status code {response.status_code}")
    
    return book_data

Save Scraped Data to Google Sheets Using Gspread

Next, push your scraped data into the Google Sheet. The function below connects using your JSON key file and updates the sheet:

import gspread

def save_books_to_google_sheet(json_key_path, spreadsheet_url, books_data):
    try:
        client = gspread.service_account(filename=json_key_path)
        sheet = client.open_by_url(spreadsheet_url).sheet1

        # Write headers
        sheet.update("A1", [["Title", "Price", "Availability"]])

        # Write book details starting from row 2
        for idx, book in enumerate(books_data, start=2):
            sheet.update(f"A{idx}", [[book["title"], book["price"], book["availability"]]])
        
        print("Data successfully written to Google Sheet!")
    except Exception as e:
        print(f"Error: {e}")

Bring It All Together

Here's how to run everything:

if __name__ == "__main__":
    json_key_path = "path/to/your/service_account.json"  # Update this
    spreadsheet_url = "https://docs.google.com/spreadsheets/d/YOUR_SPREADSHEET_ID"  # Update this

    books_data = extract_book_data()
    if books_data:
        save_books_to_google_sheet(json_key_path, spreadsheet_url, books_data)
    else:
        print("No data scraped. Check the website or your connection.")

Conclusion

Now that you've learned how to scrape book data using Python and automatically save it to Google Sheets, you can streamline your workflow and eliminate tedious manual tasks. Automation not only boosts efficiency but also lets you focus on analyzing and using your data effectively. Give it a try, experiment with the code, and watch how easy managing your scraped data can become.

About the author

SwiftProxy
Emily Chan
Lead Writer at Swiftproxy
Emily Chan is the lead writer at Swiftproxy, bringing over a decade of experience in technology, digital infrastructure, and strategic communications. Based in Hong Kong, she combines regional insight with a clear, practical voice to help businesses navigate the evolving world of proxy solutions and data-driven growth.
The content provided on the Swiftproxy Blog is intended solely for informational purposes and is presented without warranty of any kind. Swiftproxy does not guarantee the accuracy, completeness, or legal compliance of the information contained herein, nor does it assume any responsibility for content on thirdparty websites referenced in the blog. Prior to engaging in any web scraping or automated data collection activities, readers are strongly advised to consult with qualified legal counsel and to review the applicable terms of service of the target website. In certain cases, explicit authorization or a scraping permit may be required.
Join SwiftProxy Discord community Chat with SwiftProxy support via WhatsApp Chat with SwiftProxy support via Telegram
Chat with SwiftProxy support via Email