Website change monitoring using python

I wanted to monitor changes in a site. Currently I am monitoring my self-introduction text, but if I change the url and class_name, it can be used on other sites. Page data is acquired every 20 seconds, and it is displayed whether the data is different from the previous acquisition.

import requests
import time

from bs4 import BeautifulSoup


url = "https://qiita.com/sssssssiiiiinnn"
class_name ='div.newUserPageProfile_info_body.newUserPageProfile_description'
file = "elems_text.txt"


def is_not_changed(old_elem, new_elem):
    return old_elem == new_elem


def set_old_elems():
    try:
        f = open(file)
        old_elems = f.read()
        print(f'{"old_elem":10} : {old_elems}')
    except:
        old_elems = ''
    return old_elems


def set_new_elems():
    response = requests.get(url)
    response.encoding = response.apparent_encoding
    bs = BeautifulSoup(response.text, 'html.parser')
    new_elems = str(bs.select(class_name))
    print(f'{"new_elem":10} : {new_elems}')
    return new_elems


def display_result(old_elem, new_elem):
    if not is_not_changed(old_elems, new_elems):
        f = open(file, 'w')
        f.writelines(new_elems)
        f.close()
        print("Change is detected!!")
    else:
        print("not changed...")


if __name__ == '__main__':
    try:
        while(1):
            print("="*100)
            new_elems = set_new_elems()
            old_elems = set_old_elems()
            display_result(old_elems, new_elems)
            time.sleep(20)
    except KeyboardInterrupt:
        print("Interrupted by Ctrl + C")

Recommended Posts

Website change monitoring using python
Change python version using pyenv
Start using Python
Scraping a website using JavaScript in Python
Scraping using Python
Python interpreter prompt change
Operate Redmine using Python Redmine
Data cleaning using Python
Using Python #external packages
WiringPi-SPI communication using Python
Age calculation using python
Search Twitter using Python
Name identification using python
Notes using Python subprocesses
Try using Tweepy [Python2.7]
Change Maya Python Timeline
Python notes using perl-ternary operator
Flatten using Python yield from
Scraping using Python 3.5 async / await
Save images using python3 requests
Indoor monitoring using Raspberry Pi
[S3] CRUD with S3 using Python [Python]
[Python] Try using Tkinter's canvas
Try using Kubernetes Client -Python-
Login to website in Python
Python notes using perl-special variables
[Python] Using OpenCV with Python (Basic)
Scraping using Python 3.5 Async syntax
Post to Twitter using Python
Start to Selenium using python
Search algorithm using word2vec [python]
[Python] Change dtype with pandas
[Python] Folder monitoring with watchdog
python: Basics of using scikit-learn ①
# 1 [python3] Simple calculation using variables
Create JIRA tickets using Python
Instrument control using Python [pyvisa]
Manipulate spreadsheets locally using Python
Python memo using perl --join
Web scraping using Selenium (Python)
[Python] I tried using OpenPose
[Python] JSON validation using Voluptuous
How to change Python version
Broadcast on LINE using python
Data analysis using python pandas
Translate using googletrans in Python
Using Python mode in Processing
Using OpenCV with Python @Mac
[Python] Shooting game using pyxel
Send using Python with Gmail
Complement python with emacs using company-jedi
How to install python using anaconda
Initializing global variables using Python decorators
[Python] Loading csv files using pandas
Retry post request using python requests
Python Note: About comparison using is
[Ubuntu] [Python] Object tracking using dlib
Image capture of firefox using python
[Python] Using OpenCV with Python (Image Filtering)
Precautions when using pit in Python
Summary if using AWS Lambda (Python)