DEV Community

Cover image for Build a Web Scraper in Python That Runs on a Schedule
Sudhir Bahadure
Sudhir Bahadure

Posted on

Build a Web Scraper in Python That Runs on a Schedule

Introduction

According to a recent report, over 70% of businesses rely on data scraping to inform their marketing strategies, with the global web scraping market expected to reach $1.4 billion by 2028. In this tutorial, you will learn how to build a web scraper in Python that runs on a schedule, allowing you to automatically extract data from websites at regular intervals. To follow along, you will need basic knowledge of Python, a computer with Python installed, and a text editor or IDE.

Table of Contents

  1. Introduction
  2. Table of Contents
  3. Setup and Background
  4. Installing Required Libraries
  5. Writing the Web Scraper
  6. Scheduling the Web Scraper
  7. Real-World Application
  8. Conclusion

Setup and Background

python automation
Before we dive into the code, let's understand why web scraping is useful. Web scraping allows you to extract data from websites, which can be used for a variety of purposes such as market research, monitoring prices, or tracking changes to a website. To get started, we will use the requests and beautifulsoup4 libraries in Python. Here's an example of how you can use these libraries to send a GET request to a website and parse the HTML response:

import requests
from bs4 import BeautifulSoup

url = "https://www.example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
print(soup.title.text)
Enter fullscreen mode Exit fullscreen mode

This code sends a GET request to the specified URL, parses the HTML response using BeautifulSoup, and prints the title of the webpage.

Installing Required Libraries

To build our web scraper, we will need to install the requests, beautifulsoup4, and schedule libraries. You can install these libraries using pip:

pip install requests beautifulsoup4 schedule
Enter fullscreen mode Exit fullscreen mode

Once the libraries are installed, we can start writing our web scraper.

Writing the Web Scraper

Here's an example of how you can write a web scraper to extract data from a website:

import requests
from bs4 import BeautifulSoup
import csv

def scrape_website(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    data = []
    for item in soup.find_all('div', class_='item'):
        title = item.find('h2', class_='title').text
        price = item.find('span', class_='price').text
        data.append([title, price])
    return data

url = "https://www.example.com"
data = scrape_website(url)
with open('data.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(["Title", "Price"])
    writer.writerows(data)
Enter fullscreen mode Exit fullscreen mode

This code defines a function scrape_website that takes a URL as input, sends a GET request to the URL, parses the HTML response, and extracts the title and price of each item on the webpage. The extracted data is then written to a CSV file.

Scheduling the Web Scraper

To schedule our web scraper to run at regular intervals, we can use the schedule library. Here's an example of how you can schedule the web scraper to run every hour:

import schedule
import time

def job():
    url = "https://www.example.com"
    data = scrape_website(url)
    with open('data.csv', 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(["Title", "Price"])
        writer.writerows(data)

schedule.every(1).hours.do(job)  # run every hour

while True:
    schedule.run_pending()
    time.sleep(1)
Enter fullscreen mode Exit fullscreen mode

This code defines a function job that calls the scrape_website function and writes the extracted data to a CSV file. The schedule library is then used to schedule the job function to run every hour.

Real-World Application

Our web scraper can be used to monitor prices of products on an e-commerce website. For example, you can use it to track the prices of products on Namecheap or Hostinger. You can also use it to monitor the availability of products on a website and send notifications when a product is back in stock. Additionally, you can use a VPN like NordVPN (68% off + 3 months free) to protect your IP address while scraping websites.

Conclusion

In this tutorial, we learned how to build a web scraper in Python that runs on a schedule. Here are three specific takeaways:

  • We can use the requests and beautifulsoup4 libraries to send GET requests to websites and parse the HTML responses.
  • We can use the schedule library to schedule our web scraper to run at regular intervals.
  • We can use our web scraper to monitor prices of products on e-commerce websites and send notifications when a product is back in stock. To learn more about Python automation, check out the next article in the Python Automation Mastery series. ---

💡 Found this helpful?

If this tutorial saved you time or solved a problem, consider:

Support me on Ko-fi

Every coffee keeps me writing free tutorials like this one!


This article was written with AI assistance and reviewed for technical accuracy.
Part of the **Python Automation Mastery* series — Follow for more free tutorials*

#aBotWroteThis

Top comments (0)