forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
66 lines (57 loc) · 2.2 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import sys
import requests
from bs4 import BeautifulSoup
import time
def display_content(url, selector):
try:
# Send a GET request to the URL
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Create a BeautifulSoup object with the page content
soup = BeautifulSoup(response.text, 'html.parser')
# Find all elements that match the CSS selector
elements = soup.select(selector)
# Return the content of the matched elements
return [element.text for element in elements]
else:
print("Failed to fetch the webpage.")
except requests.exceptions.RequestException as e:
print("Error occurred while making the request:", e)
except Exception as e:
print("An error occurred:", e)
if __name__ == "__main__":
# Check if URL, selector, and interval are provided as arguments
if len(sys.argv) < 4:
print(
"Usage: python script.py [URL] [CSS selector] [Interval in minutes]")
sys.exit(1)
# Get the URL, selector, and interval from command-line arguments
url = sys.argv[1]
selector = sys.argv[2]
interval_minutes = int(sys.argv[3])
# Store the initial contents
initial_contents = display_content(url, selector)
if initial_contents:
print("Initial contents:")
for content in initial_contents:
print(content)
else:
print("No matching elements found.")
while True:
# Wait for the specified interval
time.sleep(interval_minutes * 60)
# Check for content changes
current_contents = display_content(url, selector)
if current_contents:
# Compare with the initial contents
if current_contents != initial_contents:
print("Content has changed!")
for content in current_contents:
print(content)
# Update the initial contents with the current contents
initial_contents = current_contents
else:
print("Content has not changed.")
else:
print("No matching elements found.")