在粘贴的代码的顶部,我有python编码,用于在网页上查找特定单词,并根据在网页上是否找到“ Food”一词返回“找到食物”或“找不到食物” 。编码的第二部分向gmail发送电子邮件,而gmail则将代码中包含的所有预写文本发送给我的手机。我分别编写了两组代码,它们分别工作,现在我试图将它们组合在一起。当最上面的代码集返回“找到食物”时,我希望最下面的代码集发送电子邮件。我不知道在阳光下搜寻所有东西后该怎么做。
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import smtplib
# if you don't want to see, how browser opens page, use headless flag
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(ChromeDriverManager().install(),
options=chrome_options)
words = ['Food']
driver.get('https://www.msn.com/')
src = driver.page_source
for word in words:
if word in src:
print(word, "was found")
else:
print(word, "was not found")
if word in src:
word = word, "was found"
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login("testing123@gmail.com", "testing123")
# message to be sent
message = "The burgers are on the MSN website."
# sending the mail
s.sendmail("testing123@gmail.com", "5555555555@txt.att.net", message)
# terminating the session
我希望一旦获得正确的编码,当在网站上找到该单词时,它将触发代码的第二部分发送电子邮件。
答案 0 :(得分:1)
因此,在找到word
之后,您可以随即发送电子邮件。基本上是这样的
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import smtplib
# function that sends mail
def send_mail(sender, receiver, message):
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login("testing123@gmail.com", "testing123")
# sending the mail
s.sendmail(sender, reciever, message)
# if you don't want to see, how browser opens page, use headless flag
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(ChromeDriverManager().install(),
options=chrome_options)
words = ['Food']
driver.get('https://www.msn.com/')
src = driver.page_source
for word in words:
if word in src:
print(word, "was found")
# send mail here
send_mail("testing123@gmail.com", "5555555555@txt.att.net", "The burgers are on the MSN website.")
else:
print(word, "was not found")
if word in src:
word = word, "was found"
答案 1 :(得分:0)
这应该有效:
if ('Food', 'was found') in words:
#start the email sending process