在函数之间传递两个变量

尼维拉尔

编辑我弄得一团糟!真抱歉!问题与最后2条印刷声明无关。当我用尽主意时添加了那些。整个问题是关于传递titlefinal_price形式check_price,以send_email在身体和主题中使用它们。

我刚刚撞墙,不知道在这里找到答案。我曾尝试根据有关YouTube的一些指南来创建网页抓取工具。但是由于我缺乏知识和经验,我陷入了如何传递2变量title以及final_price函数之间check_price()传递函数的问题send_mail()

如果我尝试仅使用纯文本发送不带这些变量的电子邮件,那么一切都会正常进行。

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)

编辑问题已解决。我先生感谢您的协助。

下面的固定和清理代码。

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()

后来我碰到墙,在那儿Send_email()产生了ValueError: Header values may not contain linefeed or carriage return characters由变量引起的错误title(它没有从上面和下面的所有空行中剥离出来。

再一次感谢大家。

拉屈霍塔布

我相信你得到的变量的方式titlefinal_price是蛮好的,它应该工作。

然而,近两年版画不应该工作,因为它们是外面的SEND_EMAIL()函数,因此titlefinal_price是未知的。您是否尝试过将打印内容放入函数中并调用它?

更新:

我现在看到错误了,您正在send_email()内部调用check_price()check_price()内部调用send_email(),这可能导致永无止境的循环

为了解决这个问题,如果您要调用的函数是check_price(),则可以使send_email()两个参数接受并在调用时传递它们。因此,check_price()您将执行以下操作:

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

然后send_email如下所示:

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

更新2:

您还可以在单​​引号字符串中使用撇号:

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

你应该做:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章