前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >每日一邮

每日一邮

作者头像
客怎眠qvq
发布2022-11-01 15:53:55
4320
发布2022-11-01 15:53:55
举报
文章被收录于专栏:某菜鸟の小屋

前言

最近在CSDN上发现许多使用Python自动发送邮件追女神的整活博客,于是想顺手做一个每天定时发送邮件给朋友的程序,内容包括舔狗日记、每天的天气变化、及人文问候。

注意

  • 测试环境: win10 python3.6
  • 第三方库 requests、yagmail、lxml
  • 使用yagmail需要开通个人发送邮箱的SMTP服务,以QQ邮箱为例:
  • 如果xdm需要发送给多人(懂的都懂),可以使用代码最后的注释块,当然天气的内容仅适用于同地区的人,异地你舔不到……
  • 可以添加更多的内容,如土味情话等;也可以打包为.exe,在服务器上每天定时运行。

代码

代码语言:javascript
复制
import requests
from lxml import etree
import yagmail

# 调用舔狗日记的api 获取内容
def tiangouApi():
    url = "https://api.dzzui.com/api/tiangou"
    result = requests.get(url).text
    lis = result.split('<br>')
    return lis[1]

# 登录邮箱,设置登录的账号,密码和port等信息
def send_email(contents, send_to):
    yag = yagmail.SMTP(user='你的邮箱',
                       password='你的授权码',
                       host='发信服务器,如使用QQ邮箱的为smtp.qq.com')

    # 设置发送给谁,邮件主题,邮件内容
    yag.send(to=send_to,
             subject='每日一邮',
             contents=contents)
    print('发送成功!')
    yag.close()


def parse(url):
    text = tiangouApi()
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
    html = requests.get(url, headers=headers)
    bs = etree.HTML(html.text)

    # 今天天气相关数据:日期,星期几,天气,最低气温,最高气温
    today_date = bs.xpath('//ul[@class = "week"]/li[1]/b/text()')[0]
    today_week = bs.xpath('//ul[@class = "week"]/li[1]/span/text()')[0]
    today_weather = bs.xpath('//ul[@class = "txt txt2"]/li[1]/text()')[0]
    today_low = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[1]/b/text()')[0]
    today_high = bs.xpath(
        '//div[@class = "zxt_shuju"]/ul/li[1]/span/text()')[0]

    print(('今天是%s,%s,%s,%s~~%s度') %
          (today_date, today_week, today_weather, today_low, today_high))

    # 明天天气相关数据,维度和上述一致
    tomorrow_date = bs.xpath('//ul[@class = "week"]/li[2]/b/text()')[0]
    tomorrow_week = bs.xpath('//ul[@class = "week"]/li[2]/span/text()')[0]
    tomorrow_weather = bs.xpath('//ul[@class = "txt txt2"]/li[2]/text()')[0]
    tomorrow_low = bs.xpath('//div[@class = "zxt_shuju"]/ul/li[2]/b/text()')[0]
    tomorrow_high = bs.xpath(
        '//div[@class = "zxt_shuju"]/ul/li[2]/span/text()')[0]

    tomorrow = ('明天是%s,%s,%s,温度范围%s~%s度,温差%d度。') % \
        (tomorrow_date, tomorrow_week, tomorrow_weather, tomorrow_low,
         tomorrow_high, int(int(tomorrow_high)-int(tomorrow_low)))


    # 计算今明两天温度差异,这里用的是最高温度
    temperature_distance = int(tomorrow_high) - int(today_high)

    if temperature_distance > 0:
        a = '明日升温%d,记得配好美美的穿搭哦,小可爱。' % temperature_distance
    elif temperature_distance < 0:
        a = '明日降温%d,记得自己添衣服,我不在的日子记得照顾好自己。' % temperature_distance
    else:
        a = '最高气温不变,在每一个平凡的日子里,也要记得翩翩起舞哦!'

    content = str(tomorrow) + str(a)
    content = text + '<br>' + content
    return content

# 发送给一人
url = "收件人所在地区天气网站"   # 代码使用的是天气网的信息 如青岛 为 https://www.tianqi.com/qingdao/
contents = parse(url)
send_to = '收件人邮箱地址'
send_email(contents, send_to)


'''
# 当然 如果你想要的给一群人发送 可使用如下代码 在emails中添加对应邮箱地址即可
emails = ['123@qq.com', '456@qq.com', '', '']
for email in emails:
    send_email(contents, email)

'''

效果

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-01-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 注意
  • 代码
  • 效果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档