DEV Community

GetUrlMonitor
GetUrlMonitor

Posted on

Building a Robust URL Monitoring System with Python and Scrapy

Introduction to URL Monitoring

URL monitoring involves tracking changes to web pages, which can be useful for various applications such as price tracking, job alerts, and competitor website analysis. In this article, we will discuss the technical aspects of building a robust URL monitoring system using Python and Scrapy.

The Importance of Web Scraping

Web scraping is a crucial component of URL monitoring, as it allows us to extract data from web pages. However, web scraping can be challenging due to the dynamic nature of web content and the need to handle anti-scraping measures. To overcome these challenges, we can use Scrapy, a powerful Python framework for web scraping.

Setting Up a Scrapy Project

To set up a Scrapy project, we need to install Scrapy using pip: pip install scrapy. Then, we can create a new Scrapy project using the command scrapy startproject url_monitor. This will create a basic directory structure for our project, including a spiders directory where we can define our spiders.

Defining a Spider

A spider is a Python class that defines how to scrape a web page. We can define a spider by creating a new Python file in the spiders directory and defining a class that inherits from scrapy.Spider. For example:

import scrapy

class UrlSpider(scrapy.Spider):
    name = "url_spider"
    start_urls = [
        'https://example.com',
    ]

    def parse(self, response):
        # Extract data from the web page
        title = response.css('title::text').get()
        yield {
            'title': title,
        }
Enter fullscreen mode Exit fullscreen mode

Handling Content Changes

To detect content changes, we need to store the previous version of the web page and compare it with the current version. We can use a database such as SQLite to store the previous versions. For example:

import sqlite3

class UrlSpider(scrapy.Spider):
    # ...

    def parse(self, response):
        # Extract data from the web page
        title = response.css('title::text').get()
        # Store the current version in the database
        conn = sqlite3.connect('urls.db')
        c = conn.cursor()
        c.execute('INSERT INTO urls (url, title) VALUES (?, ?)', (response.url, title))
        conn.commit()
        conn.close()
        # Compare the current version with the previous version
        previous_title = self.get_previous_title(response.url)
        if previous_title != title:
            # Send an email alert
            self.send_email_alert(response.url, title)

    def get_previous_title(self, url):
        conn = sqlite3.connect('urls.db')
        c = conn.cursor()
        c.execute('SELECT title FROM urls WHERE url = ?', (url,))
        previous_title = c.fetchone()
        conn.close()
        return previous_title[0] if previous_title else None

    def send_email_alert(self, url, title):
        # Send an email alert using a library such as smtplib
        import smtplib
        from email.mime.text import MIMEText
        msg = MIMEText(f'Content changed: {url}')
        msg['Subject'] = 'Content Changed'
        msg['From'] = 'your_email@example.com'
        msg['To'] = 'recipient_email@example.com'
        server = smtplib.SMTP('your_smtp_server')
        server.sendmail('your_email@example.com', 'recipient_email@example.com', msg.as_string())
        server.quit()
Enter fullscreen mode Exit fullscreen mode

Scheduling the Spider

To run the spider at regular intervals, we can use a scheduler such as schedule. For example:

import schedule
import time

def run_spider():
    spider = UrlSpider()
    spider.start()

schedule.every(1).hours.do(run_spider)  # Run the spider every hour

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

Conclusion

In this article, we discussed the technical aspects of building a robust URL monitoring system using Python and Scrapy. We defined a spider to extract data from web pages, stored the previous versions in a database, and compared the current version with the previous version to detect content changes. We also scheduled the spider to run at regular intervals using a scheduler. If you're interested in using a pre-built URL monitoring system, you can check out GetURLMonitor, which offers a free plan with 3 URLs and a pro plan with unlimited URLs and hourly checks.

Top comments (0)