Python 定时发送【每日天气】和【每日简报】至【邮件】或【钉钉】(代码部署在云服务器)

思路:
1、爬取当前所在城市的天气(实时)
2、爬取每天一分钟,知晓天下事,新闻简报(最新一天)
3、利用python将天气和简报发送至指定邮箱 或 发送至钉钉群(借助自定义机器人)
4、在本地测试通过后,就可以把代码部署到云函数中,设置定时执行。


文章给的就是一个完整代码,可能有些地方写的比较冗余,也懒得改了


每日天气:https://www.tianqi.com/chongqing/
在这里插入图片描述

每日简报:https://www.163.com/dy/media/T1603594732083.html

在这里插入图片描述

1 python 发送至邮件

参考:
Python实现自动发送邮件
Python 自动发送邮件详细教程

对代码进行了修改,然后加了每日天气和每日简报的爬虫程序

# -*- coding: utf-8 -*-
"""
Created on Fri Dec 24 15:21:51 2021

@author: ABC
"""

# smtplib 用于邮件的发信动作
import smtplib
from email.mime.text import MIMEText
# email 用于构建邮件内容
from email.header import Header
# 用于构建邮件头
import json
import hashlib
import base64
import hmac
import os
import time
import requests
from urllib.parse import quote_plus
from lxml import etree
import urllib.request  #提取网址数据
import gzip


def get_weather_data() :
    city_name = '重庆'
    url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
    weather_data = urllib.request.urlopen(url1)  
    weather_data = weather_data.read()  
    weather_data = gzip.decompress(weather_data).decode('utf-8')
    weather_dict = json.loads(weather_data)
    return weather_dict

 
def show_weather(weather_data):  
    #将形参数据值即(return weather_dict)赋值给这里的weather_dict变量
    import requests
    from lxml import etree
    base_url = 'https://www.tianqi.com/chongqing/'
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
               +'Chrome/62.0.3202.94 Safari/537.36'}
    res = requests.get(base_url,headers = headers)
    html = res.text
    html = etree.HTML(html)
    riqi  = html.xpath('//dl[@class="weather_info"]/dd[@class="week"]/text()')[0].replace(' ',' ')
    tianqi  = html.xpath('//dl[@class="weather_info"]/dd[@class="weather"]/span/b/text()')[0]
    now  = ''.join(html.xpath('//dl[@class="weather_info"]/dd[@class="weather"]/p[@class="now"]//text()'))
    wendu  = html.xpath('//dl[@class="weather_info"]/dd[@class="weather"]/span/text()')[0]
    shidu  = html.xpath('//dl[@class="weather_info"]/dd[@class="shidu"]/b[1]/text()')[0]
    shidu1  = html.xpath('//dl[@class="weather_info"]/dd[@class="shidu"]/b[2]/text()')[0]
    shidu2  = html.xpath('//dl[@class="weather_info"]/dd[@class="shidu"]/b[3]/text()')[0]
    kongqi  = html.xpath('//dl[@class="weather_info"]/dd[@class="kongqi"]/h5/text()')[0]
    pM  = html.xpath('//dl[@class="weather_info"]/dd[@class="kongqi"]/h6/text()')[0]
    richu  = html.xpath('//dl[@class="weather_info"]/dd[@class="kongqi"]/span/text()')[0]
    richu2  = html.xpath('//dl[@class="weather_info"]/dd[@class="kongqi"]/span/text()')[1]
    yu = html.xpath('//div[@class="weather_life300"]/ul/li[1]/a/p/text()')[0]
    weather_dict = weather_data
    if weather_dict.get('desc') == 'invilad-citykey':
        print('你输入的城市名有误,或者天气中心未收录你所在城市')
    elif weather_dict.get('desc') =='OK':
        forecast = weather_dict.get('data').get('forecast')

        if '雨' in yu:
            tishi = '今日可能下雨,记得带伞哦!'+weather_dict.get('data').get('ganmao')
        else:
            tishi = +weather_dict.get('data').get('ganmao')

        b = '------重庆天气查询------\n' + \
        riqi +'\n'+'天气:' + tianqi+'\n'+ '当前气温:'+ now+'\n'+ '温度:'+ wendu+'\n'+ shidu+'\n'+shidu1+'\n'+shidu2+'\n'+kongqi+'\n'+\
        pM+'\n'+richu+'\n'+richu2+ '\n'\
        '-----------------------' + '\n'\
        '明日天气:',forecast[1].get('type') + '\n' \
        '最高气温:',forecast[1].get('high').replace('高温 ','') +'\n'\
        '最低气温:',forecast[1].get('low').replace('低温 ','') +'\n'\
        '温馨提示:',tishi
        return b


def jianbao():
    base_url = 'https://www.163.com/dy/media/T1603594732083.html'
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
               +'Chrome/62.0.3202.94 Safari/537.36'}
    res = requests.get(base_url,headers = headers)
    html = res.text
    html = etree.HTML(html)
    ii_list  = html.xpath('//li[@class="media_article"]')
    url = ii_list[0].xpath('.//a[@class="media_article_img"]/@href')[0]
    res2 = requests.get(url,headers = headers)
    html2 = res2.text
    html2 = etree.HTML(html2)
    di_tie = html2.xpath('.//div[@class="post_body"]/p[@id="0H187OLR"]/text()')
    a = '\n'.join(di_tie)
    a = a.replace('365资讯简报,每天精选15条热点新闻简报1条微语,','')
    return a


if __name__ == "__main__":
    # 发信方的信息:发信邮箱,QQ 邮箱授权码
    from_addr = 'xxxxx@qq.com'
    password = 'xxxxx'
    # 收信方邮箱
    to_addr = 'xxxxx@qq.com'
    # 发信服务器
    smtp_server = 'smtp.qq.com'
    # 邮箱正文内容,第一个参数为内容,第二个参数为格式(plain 为纯文本),第三个参数为编码
    
    b = show_weather(get_weather_data())
    b = '下午好,xx\n' + "".join(b)
    a = jianbao()
    c = b +'\n' + '\n' + '------------------------------------\n'  + a
    msg = MIMEText(eval('c'),'plain','utf-8')
    # 邮件头信息
    msg['From'] = Header(from_addr)
    msg['To'] = Header(to_addr)
    msg['Subject'] = Header('Never say die.下午好,xx')
    # 开启发信服务,这里使用的是加密传输
    server=smtplib.SMTP_SSL(smtp_server)
    server.connect(smtp_server,465)
    # 登录发信邮箱
    server.login(from_addr, password)
    # 发送邮件
    server.sendmail(from_addr, to_addr, msg.as_string())
    # 关闭服务器
    server.quit()

2 python 发送至钉钉

这里也是参考Python实用宝典做的

链接:10分钟教你用Python开发钉钉通知机器人
7行代码实现早上出门前自动收到分时天气预报

里面有详细的步骤,这里我就赘述了,我只是对发送的内容作了改变

也是加了每日天气和每日简报

# -*- coding: utf-8 -*-
"""
Created on Fri Dec 24 16:34:04 2021

@author: ABC
"""
import json
import hashlib
import base64
import hmac
import os
import time
import requests
from urllib.parse import quote_plus
from lxml import etree
import urllib.request  #提取网址数据
import gzip


class Messenger:
    def __init__(self, token=os.getenv("DD_ACCESS_TOKEN"), secret=os.getenv("DD_SECRET")):
        self.timestamp = str(round(time.time() * 1000))
        self.URL = "https://oapi.dingtalk.com/robot/send"
        self.headers = {'Content-Type': 'application/json'}
        secret = secret
        secret_enc = secret.encode('utf-8')
        string_to_sign = '{}\n{}'.format(self.timestamp, secret)
        string_to_sign_enc = string_to_sign.encode('utf-8')
        hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
        self.sign = quote_plus(base64.b64encode(hmac_code))
        self.params = {'access_token': token, "sign": self.sign}

    def send_text(self, content):
        """
        发送文本
        @param content: str, 文本内容
        """
        data = {"msgtype": "text", "text": {"content": content}}
        self.params["timestamp"] = self.timestamp
        return requests.post(
            url=self.URL,
            data=json.dumps(data),
            params=self.params,
            headers=self.headers
        )
    
    def get_weather_data(self) :
        city_name = '重庆'
        url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
        weather_data = urllib.request.urlopen(url1) 
        weather_data = weather_data.read()  
        weather_data = gzip.decompress(weather_data).decode('utf-8')
        weather_dict = json.loads(weather_data)
        return weather_dict

 
    def show_weather(self,weather_data):  
        #将形参数据值即(return weather_dict)赋值给这里的weather_dict变量
        import requests
        from lxml import etree
        base_url = 'https://www.tianqi.com/chongqing/'
        headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                   +'Chrome/62.0.3202.94 Safari/537.36'}
        res = requests.get(base_url,headers = headers)
        html = res.text
        html = etree.HTML(html)
        riqi  = html.xpath('//dl[@class="weather_info"]/dd[@class="week"]/text()')[0].replace(' ',' ')
        tianqi  = html.xpath('//dl[@class="weather_info"]/dd[@class="weather"]/span/b/text()')[0]
        now  = ''.join(html.xpath('//dl[@class="weather_info"]/dd[@class="weather"]/p[@class="now"]//text()'))
        wendu  = html.xpath('//dl[@class="weather_info"]/dd[@class="weather"]/span/text()')[0]
        shidu  = html.xpath('//dl[@class="weather_info"]/dd[@class="shidu"]/b[1]/text()')[0]
        shidu1  = html.xpath('//dl[@class="weather_info"]/dd[@class="shidu"]/b[2]/text()')[0]
        shidu2  = html.xpath('//dl[@class="weather_info"]/dd[@class="shidu"]/b[3]/text()')[0]
        kongqi  = html.xpath('//dl[@class="weather_info"]/dd[@class="kongqi"]/h5/text()')[0]
        pM  = html.xpath('//dl[@class="weather_info"]/dd[@class="kongqi"]/h6/text()')[0]
        richu  = html.xpath('//dl[@class="weather_info"]/dd[@class="kongqi"]/span/text()')[0]
        richu2  = html.xpath('//dl[@class="weather_info"]/dd[@class="kongqi"]/span/text()')[1]
        yu = html.xpath('//div[@class="weather_life300"]/ul/li[1]/a/p/text()')[0]
        weather_dict = weather_data
        if weather_dict.get('desc') == 'invilad-citykey':
            print('你输入的城市名有误,或者天气中心未收录你所在城市')
        elif weather_dict.get('desc') =='OK':
            forecast = weather_dict.get('data').get('forecast')
            if '雨' in yu:
                tishi = '今日可能下雨,记得带伞哦!'+weather_dict.get('data').get('ganmao')
            else:
                tishi = +weather_dict.get('data').get('ganmao')
            b = '------重庆天气查询------\n' + \
	            riqi +'\n'+'天气:' + tianqi+'\n'+ '当前气温:'+\
	            now+'\n'+ '温度:'+ wendu+'\n'+ \
	            shidu+'\n'+shidu1+'\n'+shidu2+'\n'+kongqi+'\n'+\
	            pM+'\n'+richu+'\n'+richu2+ '\n'\
	            '-----------------------' + '\n'\
	            '明日天气:',forecast[1].get('type') + '\n' \
	            '最高气温:',forecast[1].get('high').replace('高温 ','') +'\n'\
	            '最低气温:',forecast[1].get('low').replace('低温 ','') + '\n' + '\n'\
	            '温馨提示:',tishi
            return b


    def jianbao(self):
        base_url = 'https://www.163.com/dy/media/T1603594732083.html'
        headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                   +'Chrome/62.0.3202.94 Safari/537.36'}
        res = requests.get(base_url,headers = headers)
        html = res.text
        html = etree.HTML(html)
        ii_list  = html.xpath('//li[@class="media_article"]')
        url = ii_list[0].xpath('.//a[@class="media_article_img"]/@href')[0]
        res2 = requests.get(url,headers = headers)
        html2 = res2.text
        html2 = etree.HTML(html2)
        di_tie = html2.xpath('.//div[@class="post_body"]/p[@id="0H187OLR"]/text()')
        a = '\n'.join(di_tie)
        a = a.replace('365资讯简报,每天精选15条热点新闻简报1条微语,','')
        return a
    
    
if __name__ == "__main__":
    if time.localtime().tm_hour <12:
        m = Messenger(
            token="xxx",
            secret="xxx"
        )
        b = m.show_weather(m.get_weather_data())
        b = '早上好,xx,xx,xx,xx\n' + "".join(b)
        m.send_text(eval('b'))
        a = m.jianbao()
        m.send_text(eval('a'))
    else:
        m = Messenger(
            token="xxx",
            secret="xxx"
        )
        b = m.show_weather(m.get_weather_data())
        b = '下午好,xx,xx,xx,xx\n' + "".join(b)
        m.send_text(eval('b'))

3 定时任务

这里使用的是腾讯云函数,把代码部署在云函数中,设置定时执行就行了
在这里插入图片描述

怎么操作的可以看一下其他文章(网上太多了),也懒得写了:

学校云战役自动打卡系统——基于python requests模块及腾讯云函数实现
利用云函数+Python实现每日网站自动签到并用QQ提醒
python + 腾讯云函数 实现微信公众号开发

我只是想提一下,关于云函数安装 python 第三方库的问题,其实官方文档也有很多其他方式(为Python云函数打包依赖),但我觉得这种方式最简单,最方便

参考
https://bbs.nga.cn/read.php?tid=25226439&page=e&forder_by=postdatedesc&rand=838
在这里插入图片描述

在这里插入图片描述
也就是在终端上用pip安装库,例如安装pandas,如下

pip3 install pandas --target=./src

在这里插入图片描述
在这里插入图片描述
然后部署,测试就可以啦


4 效果展示

我设置了,每天早上八点,晚上六点,定时执行

邮箱
在这里插入图片描述

钉钉
在这里插入图片描述
在这里插入图片描述

over

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值