使用变量作为电子邮件正文发送电子邮件

Sending an email using variable as email body

使用简单的网络抓取工具,我能够return将我正在寻找的内容转化为脚本中的变量content : content1

由于某种原因,我无法在电子邮件正文中显示内容。

我试过使用 message.attach(MIMEText(mail_content, 'text')) message.attach(MIMEText(mail_content, 'html')) 没有任何区别。还导入了 time 模块,因为我认为脚本中的延迟可能会有所帮助,但事实并非如此。

from selenium import webdriver
import smtplib, ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import time

DRIVER_PATH = 'C:/Users\James\Downloads\chromedriver_win32\chromedriver.exe'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
driver.implicitly_wait(5)

driver.get('https://www.skysports.com/football/news')


FootballHeadlines = driver.find_elements_by_class_name("news-list__headline-link")
for elem in FootballHeadlines:
    print(elem.text)
    print(elem.get_attribute("href"))

content  = (elem.text)
content1 = (elem.get_attribute("href"))


sender_address = 'some@email'
sender_password = '**********'
receiver_email = 'someother@email'


message = MIMEMultipart()
message['From'] = sender_address
message['To'] = sender_address
message['Subject'] = 'Latest on Football' 
session = smtplib.SMTP('smtp.gmail.com', 587) 
session.starttls()
session.login(sender_address, sender_password) 
mail_content = message.as_string(content)
message.attach(MIMEText(mail_content, 'html'))
session.sendmail(sender_address, sender_address, mail_content)

问题不在于发送电子邮件。出于某种原因,selenium 无法从它找到的元素中获取文本。我用漂亮的汤代替。以下代码有效。

import bs4 as Bs
import smtplib, ssl
from selenium import webdriver
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

DRIVER_PATH = 'D:\Webdriver\chromedriver.exe'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
driver.implicitly_wait(5)

driver.get('https://www.skysports.com/football/news')

page_src = Bs.BeautifulSoup(driver.page_source)
links = page_src.findAll("a", {"class" : "news-list__headline-link"})

content = ''
for link in links:
    content +='\n'+link.text
    
print(content)

sender_address = 'yatint5@gmail.com'
sender_password = 'ehdqxsdsuyuicupa'
receiver_email = sender_address


message = MIMEMultipart()
message['From'] = sender_address
message['To'] = sender_address
message['Subject'] = 'Latest on Football' 
message.attach(MIMEText(content, 'plain'))


session = smtplib.SMTP('smtp.gmail.com', 587) 
session.starttls()
session.login(sender_address, sender_password) 
mail_content = message.as_string(content)
print(mail_content)
message.attach(MIMEText(mail_content, 'html'))
session.sendmail(sender_address, sender_address, mail_content)

PS: 我已经撤销了 app-password...