[Python] 纯文本查看 复制代码
import tkinter as tk
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time
import threading
import ast
class jiemian():
def __init__(self):
self.root = tk.Tk()
self.root.title('QQ邮箱发送热点新闻')
self.size = self.root.geometry('300x400')
self.sende_email_label = tk.Label(self.root, text='发送邮箱').place(x=0, y=0)
self.send_get_label = tk.Label(self.root, text='授权码').place(x=0, y=20)
self.set_subject_label = tk.Label(self.root, text='邮件主题').place(x=0, y=40)
self.send_get_name_label = tk.Label(self.root, text='发件人名').place(x=0, y=60)
self.accept_name_label = tk.Label(self.root, text='收件人名').place(x=0, y=80)
self.get_time_label = tk.Label(self.root, text='新闻时间').place(x=0, y=100)
self.get_news_label = tk.Label(self.root, text='新闻内容').place(x=0, y=120)
self.get_urls_label = tk.Label(self.root, text='新闻网址').place(x=0, y=140)
self.get_email_label = tk.Label(self.root, text='接受邮箱').place(x=0, y=190)
self.send_email_entry = tk.Entry(self.root)
self.send_email_entry.place(x=55, y=0)
self.send_get_entry = tk.Entry(self.root)
self.send_get_entry.place(x=55, y=20)
self.set_subject_entry = tk.Entry(self.root)
self.set_subject_entry.place(x=55, y=40)
self.send_get_name_entry = tk.Entry(self.root)
self.send_get_name_entry.place(x=55, y=60)
self.accept_name_entry = tk.Entry(self.root)
self.accept_name_entry.place(x=55, y=80)
self.get_time_entry = tk.Entry(self.root, text='爬取新闻时间规则')
self.get_time_entry.place(x=55, y=100)
self.get_news_entry = tk.Entry(self.root, text='爬取新闻内容规则')
self.get_news_entry.place(x=55, y=120)
self.get_urls_entry = tk.Entry(self.root)
self.get_urls_entry.place(x=55, y=140)
self.get_email_entry = tk.Entry(self.root)
self.get_email_entry.place(x=55, y=190)
self.start = tk.Button(self.root, text='开始发送', command=lambda: jiemian.start_thread(self))
self.start.place(x=55, y=160)
self.read = tk.Button(self.root, text='导入上次配置', command=lambda: jiemian.read(self))
self.read.place(x=120, y=160)
self.root.mainloop()
def start(self):
self.user = {
'send_email': self.send_email_entry.get(),
'send_get': self.send_get_entry.get(),
'set_subject': self.set_subject_entry.get(),
'send_get_name': self.send_get_name_entry.get(),
'accept_name': self.accept_name_entry.get(),
'get_time': self.get_time_entry.get(),
'get_news': self.get_news_entry.get(),
'get_urls': self.get_urls_entry.get(),
'get_email':self.get_email_entry.get()
}
if self.user['get_urls']!='' :
self.users = str(self.user)
f = open('dict.txt', 'w')
f.writelines(self.users)
f.close()
chrome_options = Options()
chrome_options.add_argument('--headless')
b = webdriver.Chrome(options=chrome_options)
while True:
b.get(self.user['get_urls'])
r_time = b.find_element_by_xpath(self.user['get_time']).text
r_text = b.find_element_by_xpath(self.user['get_news']).text
really_text = r_time + '\n' + r_text
text = ''
print(really_text)
try:
if really_text != text:
text = really_text
mail_host = "smtp.qq.com" # 设置的邮件服务器host必须是发送邮箱的服务器,与接收邮箱无关。
mail_user = self.user['send_email'] # qq邮箱登陆名
mail_pass = self.user['send_get'] # 开启stmp服务的时候并设置的授权码,注意!不是QQ密码。
sender = self.user['send_email'] # 发送方qq邮箱
receivers = self.users_['get_email'] # 接收方qq邮箱
message = MIMEText(text, 'plain', 'utf-8')
message['From'] = Header(self.user['send_get_name'], 'utf-8') # 设置显示在邮件里的发件人
message['To'] = Header(self.user['accept_name'], 'utf-8') # 设置显示在邮件里的收件人
subject = self.user['set_subject']
message['Subject'] = Header(subject, 'utf-8') # 设置主题和格式
try:
smtpobj = smtplib.SMTP_SSL(mail_host, 465) # 本地如果有本地服务器,则用localhost ,默认端口25,腾讯的(端口465或587)
smtpobj.set_debuglevel(1)
smtpobj.login(mail_user, mail_pass) # 登陆QQ邮箱服务器
smtpobj.sendmail(sender, receivers, message.as_string()) # 发送邮件
smtpobj.quit() # 退出
print("邮件发送成功")
time.sleep(300)
b.refresh()
continue
except smtplib.SMTPException as e:
print("Error:无法发送邮件")
print(e)
b.refresh()
continue
except:
print('热点暂未更新')
time.sleep(300)
b.refresh()
continue
def start_thread(self):
t = threading.Thread(target=lambda: jiemian.start(self))
t.setDaemon(True)
t.start()
def read(self):
with open('dict.txt', 'r') as f:
self.users_ = ast.literal_eval(f.read())
self.send_email_entry.insert(0, self.users_['send_email'])
self.send_get_entry.insert(0, self.users_['send_get'])
self.set_subject_entry.insert(0, self.users_['set_subject'])
self.send_get_name_entry.insert(0,self.users_['send_get_name'])
self.accept_name_entry.insert(0, self.users_['accept_name'])
self.get_time_entry.insert(0, self.users_['get_time'])
self.get_news_entry.insert(0,self.users_['get_news'])
self.get_urls_entry.insert(0, self.users_['get_urls'])
self.get_email_entry.insert(0,self.users_['get_email'])
if __name__ == '__main__':
jiemian()