思路: 1、爬取当前所在城市的天气(实时) 2、爬取每天一分钟,知晓天下事,新闻简报(最新一天) 3、利用python将天气和简报发送至指定邮箱 或 发送至钉钉群(借助自定义机器人) 4、在本地测试通过后,就可以把代码部署到云函数中,设置定时执行。
文章给的就是一个完整代码,可能有些地方写的比较冗余,也懒得改了
每日天气:https://www.tianqi.com/chongqing/
每日简报:https://www.163.com/dy/media/T1603594732083.html
1 python 发送至邮件
参考: Python实现自动发送邮件 Python 自动发送邮件详细教程
对代码进行了修改,然后加了每日天气和每日简报的爬虫程序
"""
Created on Fri Dec 24 15:21:51 2021
@author: ABC
"""
import smtplib
from email.mime.text import MIMEText
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):
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__":
from_addr = 'xxxxx@qq.com'
password = 'xxxxx'
to_addr = 'xxxxx@qq.com'
smtp_server = 'smtp.qq.com'
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行代码实现早上出门前自动收到分时天气预报
里面有详细的步骤,这里我就赘述了,我只是对发送的内容作了改变
也是加了每日天气和每日简报
"""
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):
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
|