使用if函数输出的Python
我正在运行一个脚本,打算运行x个时间段,在此期间,它正在检查一个网站,查看各种产品的库存状态是否已更新为“库存”。我的目标是在电子邮件正文中发送一封包含以下函数全部输出结果的电子邮件(不仅仅是库存产品)使用if函数输出的Python,python,function,if-statement,while-loop,Python,Function,If Statement,While Loop,我正在运行一个脚本,打算运行x个时间段,在此期间,它正在检查一个网站,查看各种产品的库存状态是否已更新为“库存”。我的目标是在电子邮件正文中发送一封包含以下函数全部输出结果的电子邮件(不仅仅是库存产品)def first_-trunt()。例如,只要至少有一种产品在库存中,我希望它是触发电子邮件的事件,然后接收整个输出,列出哪些产品在库存中,哪些缺货。而且,我不想让它停止循环 我需要帮助的是: 如何编写下一个函数,该函数表示只要存在 至少有一个输出包含“可用”的实例,请执行以下操作 某物基本上,
def first_-trunt()
。例如,只要至少有一种产品在库存中,我希望它是触发电子邮件的事件,然后接收整个输出,列出哪些产品在库存中,哪些缺货。而且,我不想让它停止循环
我需要帮助的是:
def first_-trunt()
函数中获取整个输出/打印?我意识到我需要将它存储为一个变量,然后我可以将它作为参数在SMTP设置中传递,如{},但我不知道如何使用初始传递设置它
t_end=time.time()+60*5
定义首次尝试()
now=datetime.now()
date\u time=now.strftime(“%H:%M:%S”)
如果r.文本中的“产品1”:
打印(“产品1可用”,日期\时间)
其他:
打印('产品1无库存',日期\时间)
如果r.文本中的“产品2”:
打印(“产品2可用”,日期/时间)
其他:
打印('产品2未库存',日期\时间)
如果r.文本中的“产品3”:
打印(“产品3可用”,日期/时间)
其他:
打印('产品3无库存',日期\时间)
driver.refresh()
时间。睡眠(30)
当time.time()结束时:
第一次尝试()
除非我理解错了你的问题,否则这就是你想要做的:
t_end = time.time() + 60 * 5
def first_attempt():
now = datetime.now()
return now.strftime("%H:%M:%S")
def sendEmail(date_time, content, product1, product2, product3):
pass
# send your email with the info you want
while time.time() < t_end:
date_time = first_attempt()
if "product 1" in r.text:
print('product 1 available', date_time)
product1 = [True, 'product 1 available']
else:
print('product 1 not in stock', date_time)
product1 = [False, 'product 1 not in stock']
if "product 2" in r.text:
print('product 2 available', date_time)
product2 = [True, 'product 2 available']
else:
print('product 2 not in stock', date_time)
product2 = [False, 'product 2 not in stock']
if "product 3" in r.text:
print('product 3 available', date_time)
product3 = [True, 'product 3 available']
else:
print('product 3 not in stock', date_time)
product3 = [False, 'product 3 not in stock']
if product1[0] or product2[0] or product3[0]:
# you can choose to pass whatever info you want in order to construct
# your email - here I pass the time, the content text, as well as boolean
# values (True/False) for each product telling us if it is available or not
sendEmail(date_time, r.text, product1, product2, product3)
driver.refresh()
time.sleep(30)
t_end=time.time()+60*5
定义首次尝试()
now=datetime.now()
立即返回。strftime(“%H:%M:%S”)
def sendEmail(日期、时间、内容、产品1、产品2、产品3):
通过
#发送包含所需信息的电子邮件
当time.time()结束时:
日期时间=首次尝试()
如果r.文本中的“产品1”:
打印(“产品1可用”,日期\时间)
product1=[True,‘产品1可用’]
其他:
打印('产品1无库存',日期\时间)
product1=[False,‘产品1没有库存’]
如果r.文本中的“产品2”:
打印(“产品2可用”,日期/时间)
product2=[True,‘产品2可用’]
其他:
打印('产品2未库存',日期\时间)
product2=[False,‘产品2没有库存’]
如果r.文本中的“产品3”:
打印(“产品3可用”,日期/时间)
product3=[True,‘产品3可用’]
其他:
打印('产品3无库存',日期\时间)
product3=[错误,'产品3没有库存']
如果产品1[0]或产品2[0]或产品3[0]:
#您可以选择传递任何您想要的信息,以便构建
#你的电子邮件-我在这里传递时间、内容文本以及布尔值
#每个产品的值(真/假)告诉我们是否可用
发送电子邮件(日期、时间、右文本、产品1、产品2、产品3)
driver.refresh()
时间。睡眠(30)
正如您在问题中所说的,您可以将每个产品作为一个变量传递,然后,如果有,请发送包含所有产品变量的参数的电子邮件。如果要发送content.text,只需将product#变量设置为r.text或None,然后传递这些变量
我希望这能回答你的问题。除非我理解错了你的问题,否则这就是你想要做的:
t_end = time.time() + 60 * 5
def first_attempt():
now = datetime.now()
return now.strftime("%H:%M:%S")
def sendEmail(date_time, content, product1, product2, product3):
pass
# send your email with the info you want
while time.time() < t_end:
date_time = first_attempt()
if "product 1" in r.text:
print('product 1 available', date_time)
product1 = [True, 'product 1 available']
else:
print('product 1 not in stock', date_time)
product1 = [False, 'product 1 not in stock']
if "product 2" in r.text:
print('product 2 available', date_time)
product2 = [True, 'product 2 available']
else:
print('product 2 not in stock', date_time)
product2 = [False, 'product 2 not in stock']
if "product 3" in r.text:
print('product 3 available', date_time)
product3 = [True, 'product 3 available']
else:
print('product 3 not in stock', date_time)
product3 = [False, 'product 3 not in stock']
if product1[0] or product2[0] or product3[0]:
# you can choose to pass whatever info you want in order to construct
# your email - here I pass the time, the content text, as well as boolean
# values (True/False) for each product telling us if it is available or not
sendEmail(date_time, r.text, product1, product2, product3)
driver.refresh()
time.sleep(30)
t_end=time.time()+60*5
定义首次尝试()
now=datetime.now()
立即返回。strftime(“%H:%M:%S”)
def sendEmail(日期、时间、内容、产品1、产品2、产品3):
通过
#发送包含所需信息的电子邮件
当time.time()结束时:
日期时间=首次尝试()
如果r.文本中的“产品1”:
打印(“产品1可用”,日期\时间)
product1=[True,‘产品1可用’]
其他:
打印('产品1无库存',日期\时间)
product1=[False,‘产品1没有库存’]
如果r.文本中的“产品2”:
打印(“产品2可用”,日期/时间)
product2=[True,‘产品2可用’]
其他:
打印('产品2未库存',日期\时间)
product2=[False,‘产品2没有库存’]
如果r.文本中的“产品3”:
打印(“产品3可用”,日期/时间)
product3=[True,‘产品3可用’]
其他:
打印('产品3无库存',日期\时间)
product3=[错误,'产品3没有库存']
如果产品1[0]或产品2[0]或产品3[0]:
#您可以选择传递任何您想要的信息,以便构建
#你的电子邮件-我在这里传递时间、内容文本以及布尔值
#每个产品的值(真/假)告诉我们是否可用
发送电子邮件(日期、时间、右文本、产品1、产品2、产品3)
driver.refresh()
时间。睡眠(30)
正如您在问题中所说的,您可以将每个产品作为一个变量传递,然后,如果有,请发送包含所有产品变量的参数的电子邮件。如果要发送content.text,只需将product#变量设置为r.text或None,然后传递这些变量
我希望这能回答您的问题。太好了,谢谢!今天晚些时候我会尝试,一旦我可以开始工作,我会将其标记为正确!提摩太,这个答案很好,我感谢你的帮助!我唯一的另一个问题是,对于最终的if语句,测试是否有任何东西是真的,您将如何传递原始if语句打印的结果