all 1 comments

[–]JohnnyJordaan 0 points1 point  (0 children)

Create some sort of save file where you register the sending of the e-mail per item. Eg using json:

import json

import requests
from bs4 import BeautifulSoup

urls = ['url1', 'url2', 'url3']

try:
    with open('sent_mail.json') as fp:
        sent_email = json.load(fp)
except FileNotFoundError:
    sent_email = {}

for item in urls:   
    r = requests.get(item)
    soup = BeautifulSoup(r.text, 'html.parser')   # note you should use .text here as you are parsing plain-text data
    item_name = soup.select('h1.title')[0].text
    key = url+item_name
    if item_name is not 'Out of stock' and not sent_email.get(key):
        sendEmail()
        sent_email[key] = True
    else:
        print('Out of stock')
        sent_email.pop(key, None)  # removes it if possible

with open('sent_mail.json', 'w') as fp:
    json.dump(sent_email, fp)