用Python自动发邮件提醒你周末吃啥
作者:Python进阶者 时间:2022-04-10 11:44:28
前言
前几天在Python交流群里边,大佬分享了一个有趣的代码,用于定时发送菜谱分享,觉得挺有意思,这里拿出来给大家分享。
实现思路
实现思路倒是不难,其一是Python网络爬虫,将网页上的每日一文抓取到,然后存起来到一个变量中,其二是编写发送邮件的逻辑,编辑好邮寄模板,然后将抓到的内容发送出去即可。
实现过程
这里直接上代码,如下所示:
import requests, bs4
import smtplib
import schedule
import time
from bs4 import BeautifulSoup
from email.mime.text import MIMEText
from email.header import Header
# account = input('请输入你的邮箱:')
# password = input('请输入你的密码:')
# receiver = input('请输入收件人的邮箱:')
account = '{0}'.format('请输入你的邮箱:')
password = '{0}'.format('请输入你的密码:')
receiver = '{0}'.format('请输入收件人的邮箱:')
def recipe_spider():
list_all = ''
num = 0
for a in range(1, 11):
headers = {'user-agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
n = '{0}{1}{2}'.format('https://home.meishichina.com/show-top-type-recipe-page-', a, '.html')
res_foods = requests.get(n, headers=headers)
bs_foods = bs4.BeautifulSoup(res_foods.text, 'html.parser')
list_foods = bs_foods.find('div', class_='space_left')
for food in list_foods.find_all('li'):
num = num+1
name = food.find('h2').text.strip()
foods = food.find('p', class_='subcontent').text.strip()
url_food = food.find('a')['href'].strip()
food_info = '''
%s、%s
%s
链接: %s
''' % (num, name, foods, url_food)
list_all = list_all+food_info
return (list_all)
def send_email(list_all):
global account, password, receiver
mailhost = 'smtp.qq.com'
qqmail = smtplib.SMTP_SSL(mailhost, 465)
# qqmail.connect(mailhost,465)
qqmail.login(account, password)
content = '亲爱的,本周的热门菜谱如下' + list_all
message = MIMEText(content, 'plain', 'utf-8')
subject = '周末吃个啥——美食天下'
message['Subject'] = Header(subject, 'utf-8')
try:
qqmail.sendmail(account, receiver, message.as_string())
print('邮件发送成功')
except:
print('邮件发送失败')
qqmail.quit()
def job():
print('开始一次任务')
list_all = recipe_spider()
send_email(list_all)
print('任务完成')
if __name__ == '__main__':
job()
# schedule.every(0.05).minutes.do(job)
# while True:
# schedule.run_pending()
# time.sleep(1)
你只需要输入你的邮箱,邮箱授权码和对应的收件人即可。如果不知道邮箱授权码怎么配置的话,请参考这篇文章,手把手教你使用Python网络爬虫实现邮件定时发送(附源码),有详细的配置过程,这里不再赘述。
代码运行之后,用户就可以收到你发送的邮件了,如下所示。
你也可以定时给自己发,使用定时任务工具,做个定时任务,每周提醒自己,看看该整个啥好吃的菜!
来源:https://blog.51cto.com/u_13389043/4907234
标签:Python,邮件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python处理json字符串转化为字典的简单实现
2022-05-15 01:42:58
YUI Compressor 组件压缩 JavaScript 的一些原理
2009-08-02 20:22:00
Python OpenCV超详细讲解调整大小与图像操作的实现
2022-07-09 21:16:27
![](https://img.aspxhome.com/file/2023/2/105582_0s.png)
pyTorch深入学习梯度和Linear Regression实现
2023-05-12 01:00:45
![](https://img.aspxhome.com/file/2023/5/89435_0s.png)
asp 在线备份与恢复sqlserver数据库的代码
2011-03-06 11:14:00
使用python+pandas读写xlsx格式中的数据
2023-03-25 00:55:16
![](https://img.aspxhome.com/file/2023/5/105755_0s.png)
MySQL内建复制功能来优化可用性
2010-10-25 20:20:00
Python通过正则库爬取淘宝商品信息代码实例
2021-12-09 18:49:55
![](https://img.aspxhome.com/file/2023/5/75235_0s.jpg)
python基础详解之if循环语句
2022-04-17 02:14:09
![](https://img.aspxhome.com/file/2023/0/103310_0s.png)
python 获取字典特定值对应的键的实现
2022-07-01 19:25:21
三种不同方式连接MySQL数据库的方法及示例
2010-06-11 13:37:00
Python数据可视化常用4大绘图库原理详解
2023-08-25 11:12:09
![](https://img.aspxhome.com/file/2023/7/62297_0s.jpg)
python网络爬虫实现发送短信验证码的方法
2021-03-23 23:18:54
![](https://img.aspxhome.com/file/2023/6/97596_0s.jpg)
Django操作cookie的实现
2023-06-07 17:05:21
![](https://img.aspxhome.com/file/2023/1/77011_0s.jpg)
python字符串格式化方式解析
2021-06-22 14:01:46
什么是好的设计
2010-02-25 12:22:00
通向MySQL神秘王国的图形化之路
2008-12-08 13:43:00
![](https://img.aspxhome.com/file/UploadPic/200812/200812917612229.jpg)
Python 私有化操作实例分析
2022-11-06 05:44:42
![](https://img.aspxhome.com/file/2023/9/69049_0s.png)
Python爬虫框架NewSpaper使用详解
2022-06-03 06:20:26
![](https://img.aspxhome.com/file/2023/3/71333_0s.jpg)
举例讲解Python中装饰器的用法
2022-12-26 17:02:23