Create a Web-Crawler Notification Bot in Python
Last Updated :
31 Jan, 2024
In this article, we will guide you through the process of creating a Web-Crawler Notification Bot using Python. This notification bot is designed to display notification messages on your window, providing a useful tool for alerting users to relevant information gathered through web crawling.
What is a Notification Bot?
A Notification Bot is a program designed to deliver timely alerts or messages to users. Typically automated, it serves to notify users of important events, updates, or information, enhancing user engagement and awareness. In Python, creating a Notification Bot involves utilizing web-crawling techniques to gather relevant data for instant notifications on a user’s window.
Create A Web-Crawler Notification Bot in Python
Below, are the step-by-step implementation of Web-Crawler Notification Bot in Python.
Step 1: Create a Virtual Environment
First, create the virtual environment using the below commands
python -m venv env
.\env\Scripts\activate.ps1
Step 2: Install Neccassary Library
We need to install two libraries: bs4 for BeautifulSoup and plyer for notification display. For install this library run the below command
pip install bs4
pip install plyer
Step 3: Import Neccassary Library
We , First import the two libraries: bs4 for BeautifulSoup and plyer for notification display. For import these library run the below code
Python3
from bs4 import BeautifulSoup
from plyer import notification
|
Step 4:Implement the Logic
In this example, below code first imports necessary modules such as requests for HTTP requests, BeautifulSoup for HTML parsing, and plyer for notifications. The `send_notification` function is defined to display notifications, and the `parse_html` function fetches the content of a webpage, compares it with the previous content, and sends a notification if changes are detected. The main loop in the `main` function continuously checks the specified URL (in this case, Apple’s iPhone page) for changes every hour, utilizing the functions mentioned earlier.
Python3
import requests
from bs4 import BeautifulSoup
from plyer import notification
import time
def send_notification(title, message):
notification.notify(
title = title,
message = message,
timeout = 10
)
def parse_html(url, last_content):
try :
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser' )
current_content = str (soup.find( 'div' , class_ = 'content' ).text)
if current_content ! = last_content:
send_notification( "Web Crawler Notification" , "New content detected on the webpage!" )
return current_content
except Exception as e:
print (f "Error: {e}" )
return last_content
def main():
last_content = ""
while True :
last_content = parse_html(url, last_content)
time.sleep( 3600 )
if __name__ = = "__main__" :
main()
|
Run the Server
runt the server using the below command.
python script_name.py
Output :
Conclusion
In conclusion, the provided Python script demonstrates the creation of a simple web-crawler notification bot. Using the requests library for fetching webpage content, BeautifulSoup for HTML parsing, and plyer for notifications, the script monitors a specified webpage (e.g., Apple’s iPhone page) for changes. If differences are detected, it sends a notification indicating the presence of new content.
Share your thoughts in the comments
Please Login to comment...