EDIT
I have made huge mess! I'm sincerely sorry! Issue is not about last 2 print statement. Those were added when I run out of idea. Whole issue is about passing title and final_price form check_price to send_email and use them in body and subject.
I've just hit brick wall and don't know here to find answer.
I have tried to create web scraper according to some guide on YT. However due to my lack in knowledge and experience im stuck on issue how to pass 2 variable title and final_price from function check_price() to function send_mail()
Everything will work if I'll try to send email without those variables using just plain text.
import os
import smtplib
import requests
from email.message import EmailMessage
from bs4 import BeautifulSoup
EMAIL_ADDRESS = os.environ.get('GMAIL_USER')
EMAIL_PASSWORD = os.environ.get('GMAIL_PASSWORD')
URL_AMZ = 'https://www.amazon.de/Logitech-kabelgebundene-fortschrittlicher-Muskelbelastung-fortschrittliche/dp/B07FNHV4MW/ref=sr_1_3?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=26227QRRLWQHF&keywords=logitech+mx+vertical&qid=1571861245&sprefix=logitech+mx+%2Caps%2C342&sr=8-3'
# URL_MBNK = 'https://www.mbank.pl/serwis-ekonomiczny/kursy-walut/'
URL_GGL ='https://www.google.com/search?rlz=1C1GCEU_plPL839PL839&sxsrf=ACYBGNSHqUQOq6lZRXHyeKLPAd0peUegqg%3A1571862894642&ei=brmwXevuJuaFk74P1Mi12A8&q=euro&oq=euro&gs_l=psy-ab.3..0i71l8.0.0..1236884...0.2..0.0.0.......0......gws-wiz.D2K7kmd_GB8&ved=0ahUKEwjr3eHLnbPlAhXmwsQBHVRkDfsQ4dUDCAs&uact=5'
headers = {"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36'}
def check_price():
# global title
# global final_price
page_amaz = requests.get(URL_AMZ, headers=headers)
# page_mbnk = requests.get(URL_MBNK, headers=headers)
page_ggl = requests.get(URL_GGL, headers=headers)
soup_amaz = BeautifulSoup(page_amaz.content, 'html.parser')
# soup_mbnk = BeautifulSoup(page_mbnk.content, 'html.parser')
soup_ggl = BeautifulSoup(page_ggl.content, 'html.parser')
title = soup_amaz.find(id='productTitle').get_text()
price = soup_amaz.find(id='priceblock_ourprice').get_text()
converted_price = float(price[0:-2].replace(',','.'))
# convert_ratio = soup_mbnk.find(id="currencies").get_text()
convert_ratio_ggl = soup_ggl.find('div','dDoNo vk_bk').get_text()
clean_convert_ratio = float(convert_ratio_ggl[0:4].replace(',','.'))
final_price = converted_price * clean_convert_ratio
if(final_price > 200):
send_email()
return title, final_price
def send_email():
title, final_price = check_price()
msg = EmailMessage()
msg['Subject'] = f'Zmiana ceny produktu {title}' #% (title)
msg['From'] = EMAIL_ADDRESS
msg['To'] = 'I know it's bit to late... but here was my email'
msg.set_content(f'Cena {final_price} -- Link:{URL_AMZ}') # % (final_price ,URL_AMZ))
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS , EMAIL_PASSWORD)
smtp.send_message(msg)
print("Mail Wysłany")
#print(title)
#print(final_price)
EDIT Issue was solved. I,m r thankful for all assistance.
Below fixed and cleaned code.
import requests
import smtplib
import os
from bs4 import BeautifulSoup
from email.message import EmailMessage
EMAIL_ADDRESS = os.environ.get('GMAIL_USER')
EMAIL_PASSWORD = os.environ.get('GMAIL_PASSWORD')
URL_AMZ = 'https://www.amazon.de/Logitech-kabelgebundene-fortschrittlicher-Muskelbelastung-fortschrittliche/dp/B07FNHV4MW/ref=sr_1_3?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=26227QRRLWQHF&keywords=logitech+mx+vertical&qid=1571861245&sprefix=logitech+mx+%2Caps%2C342&sr=8-3'
URL_GGL ='https://www.google.com/search?rlz=1C1GCEU_plPL839PL839&sxsrf=ACYBGNSHqUQOq6lZRXHyeKLPAd0peUegqg%3A1571862894642&ei=brmwXevuJuaFk74P1Mi12A8&q=euro&oq=euro&gs_l=psy-ab.3..0i71l8.0.0..1236884...0.2..0.0.0.......0......gws-wiz.D2K7kmd_GB8&ved=0ahUKEwjr3eHLnbPlAhXmwsQBHVRkDfsQ4dUDCAs&uact=5'
headers = {"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36'}
def check_price():
page_amaz = requests.get(URL_AMZ, headers=headers)
page_ggl = requests.get(URL_GGL, headers=headers)
soup_amaz = BeautifulSoup(page_amaz.content, 'html.parser')
soup_ggl = BeautifulSoup(page_ggl.content, 'html.parser')
title = soup_amaz.find(id='productTitle').get_text()
final_title = title.strip() # variable title contain monstrocity that contained 5 8x \r\n above and belowe title.
price = soup_amaz.find(id='priceblock_ourprice').get_text()
converted_price = float(price[0:-2].replace(',', '.'))
convert_ratio_ggl = soup_ggl.find('div','dDoNo vk_bk').get_text()
clean_convert_ratio = float(convert_ratio_ggl[0:4].replace(',','.'))
final_price = converted_price * clean_convert_ratio
if final_price > 200:
send_email(final_title, final_price)
def send_email(final_title, final_price):
msg = EmailMessage()
msg['Subject'] = f'Zmiana ceny produktu {final_title}'
msg['From'] = EMAIL_ADDRESS
msg['To'] = 'EMAIL_ADDRESS'
msg.set_content(f'Cena {final_price} -- Link:{URL_AMZ}')
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS , EMAIL_PASSWORD)
smtp.send_message(msg)
print("Mail Wysłany")
check_price()
Later i've hit wall where Send_email() generated error that
ValueError: Header values may not contain linefeed or carriage return characters
Which was caused by variable title (it was not striped from all empty lines above and below.
One more time big thank's you to all.
Traceback (most recent call last): File "/mnt/c/_KOD_/github/Web-Scrapper/Scrapper.py", line 58, in <module> print(title) NameError: name 'title' is not definedtitlenot defined