Skip to content
Advertisement

Passing two variables between functions

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 rn 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.

Advertisement

Answer

I believe your way of getting the variables title and final_price is just fine, it should work.

However, the last two prints shouldn’t work as they are outside of the send_email() function and therefore title and final_price are unknown. Have you tried putting the print inside the function and calling it?

Update:

I see the error now, you are calling send_email() inside check_price(), and check_price() inside send_email(), which can lead to a neverending loop.

To solve this, if the function you wish to call is check_price(), you could make send_email() accept two parameters and pass them when you call it. So, in check_price() you would do:

...
if(final_price > 200):
   send_email(title,final_price)
...

and then send_email would be as follows:

def send_email(title,final_price):
   msg = EmailMessage()
   ...

Update 2:

You are also using an apostrophe inside a single quote string in:

'I know it's bit to late... but here was my email'

you should do:

"I know it's bit to late... but here was my email"
Advertisement