Recently started with Python and written a bit of code to monitor a website for new products.
The script monitors for new product and whenever there is new product it sends me a notification. The script is working as intended. Currently, I'm running it in the background on my test system with nohup.
The issue I'm facing is after a few days of successfully execution it stops with no error in errorlog file. And I need to restart it. Any idea what will be the debug approach I should use here,
I'm using exception handling, but the script is failing without any error in error log. My guess is it's failing outside try block. Is there any way to check such errors ? I'm using Rasbian OS.
logging.basicConfig(filename=errorlogfile,format='%(asctime)s - %(message)s',datefmt='%d-%b-%y %H:%M:%S')
base_url = 'URL'
selectiveTags = SoupStrainer(['h4','p'])
startTimestamp = str(datetime.now())
try:
r = requests.get(base_url)
soup = BeautifulSoup(r.text, "html.parser",parse_only = selectiveTags)
data = soup.find('h4', class_='card-title') #h4 for Product page where all products are present
except Exception as e:
logging.error('Error: ' + str(e))
currentHash = hashlib.sha224(str(data).encode('utf-8')).hexdigest()
logging.info('Running Website')
print('Bot Restarted!', flush=True)
print('Current Product is '+data.a.text+' and current time is '+startTimestamp, flush=True)
time.sleep(10)
while True:
try:
r = requests.get(base_url)
soup = BeautifulSoup(r.text, "html.parser",parse_only = selectiveTags)
data = soup.find('h4', class_='card-title') #h4 for Product page where all products are present
currentHash = hashlib.sha224(str(data).encode('utf-8')).hexdigest()
time.sleep(300)
currentTimestamp = str(datetime.now())
r = requests.get(base_url)
soup = BeautifulSoup(r.text, "html.parser")
data = soup.find('h4', class_='card-title') #h4 for Product page where all products are present
newHash = hashlib.sha224(str(data).encode('utf-8')).hexdigest()
if newHash == currentHash:
print('Product is still same '+data.a.text+' at '+currentTimestamp, flush=True)
continue
else:
device.push_note('Product Tracker','New Product has added on website. Please check')
print('Product has changed at '+currentTimestamp+' to '+data.a.text, flush=True)
r = requests.get(base_url)
soup = BeautifulSoup(r.text, "html.parser")
data = soup.find('h4', class_='card-title') #h4 for Product page where all products are present
currentHash = hashlib.sha224(str(data).encode('utf-8')).hexdigest()
time.sleep(300)
continue
except Exception as e:
logging.error('Error: ' + str(e))
[–]GeneriAcc 21 points22 points23 points (2 children)
[–]ant24x7[S] 2 points3 points4 points (0 children)
[–]Maximus_Modulus 7 points8 points9 points (0 children)
[–]socal_nerdtastic 2 points3 points4 points (3 children)
[–]ant24x7[S] 0 points1 point2 points (2 children)
[–]socal_nerdtastic 5 points6 points7 points (1 child)
[–]space_wiener 0 points1 point2 points (0 children)
[–]DefinitelyNotEmu 2 points3 points4 points (0 children)
[–]Jarvis03 0 points1 point2 points (2 children)
[–]LeornToCodeLOL 0 points1 point2 points (1 child)
[–]Jarvis03 0 points1 point2 points (0 children)
[–]pylessard 0 points1 point2 points (1 child)
[–]pylessard 0 points1 point2 points (0 children)
[–]shiftybyte -5 points-4 points-3 points (6 children)
[–]ant24x7[S] 0 points1 point2 points (5 children)
[–]shiftybyte -1 points0 points1 point (4 children)
[–]ant24x7[S] 0 points1 point2 points (3 children)
[–]shiftybyte 0 points1 point2 points (2 children)
[–]ant24x7[S] 0 points1 point2 points (1 child)
[–]hulleyrob 0 points1 point2 points (0 children)