自动爬股市新闻,邮箱自动转发,这次做了个界面
import tkinter as tkfrom 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()
https://static.52pojie.cn/static/image/hrline/4.gif
发送邮箱:需要开通QQ邮箱SMTP服务
授权码:开通了SMTP服务会给你
新闻时间规则://*[@id="kxlist"]/div/div
新闻内容规则://*[@id="kxlist"]/div/div/div/div/span
新闻网址:http://m.eastmoney.com/kuaixun
接受邮箱:想要接受的邮箱(如果收不到需要将发送邮箱加入白名单)
规则:XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言。(百度百科){:1_918:} xufanz 发表于 2021-1-16 13:55
运行了一下,每5分钟发一次邮件,没更新消息的话会一直发重复的邮件
可以自己调整一下,把第74行代码放到68行下面,这个弄错了 xufanz 发表于 2021-1-16 13:55
运行了一下,每5分钟发一次邮件,没更新消息的话会一直发重复的邮件
忘了检测内容重复了 支持一下!!!! 这个好,就是不知道怎么用 楼主发的这个分享太棒了,太实用啦{:301_995:}~! 这个要学习下,感谢楼主给力分享 爬虫还真是厉害呀。 楼主的分享很实用!谢谢了! 小白表示 想白P 个成品哈哈 人生苦短我学Python 感谢分享