使用一个简单的webscraper,我可以将要查找的内容返回到脚本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)
答案 0 :(得分:2)
问题不在于发送电子邮件。由于某种原因,硒无法从其发现的元素中提取出文字。我改用漂亮的汤。以下代码有效。
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:我已经撤消了应用密码...