通过天气网制作专属天气预报
本帖最后由 kuruite 于 2020-11-23 11:26 编辑# -*- coding: utf-8 -*-
"""
learn python:tianqi
<通过天气网的接口,制作专属天气预报,可以对接树莓派哦,搞个屏幕>
:copyright: (c) 2019 by learn python.
:license: GPLv3, see LICENSE File for more details.
"""
import requests
from lxml import etree
requests.packages.urllib3.disable_warnings()
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0"
}
def getText(elem):
rc = []
for node in elem.itertext():
rc.append(node.strip())
return "".join(rc)
def get_weather_info(addr):
global weather_info, today, date
url = "https://www.tianqi.com/" + str(addr)
responese = requests.get(url, headers=headers, verify=False)
html = responese.text
selector = etree.HTML(html)
try:
name = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="name"]/h2/text()'
)
date_rili = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="week"]/text()'
)
now_temp = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="weather"]/p[@class="now"]/b/text()'
)
now_w = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="weather"]/span/b/text()'
)
today_temp = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="weather"]/span/text()'
)
dampness = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="shidu"]/b/text()'
)
now_wind = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="shidu"]/b/text()'
)
now_sunshine = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="shidu"]/b/text()'
)
air = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="kongqi"]/h5/text()'
)
pm = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="kongqi"]/h6/text()'
)
node = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="kongqi"]/span'
)
sun_s = getText(node)
sun_richu = sun_s.split("日落")
sun_riluo = "日落" + str(sun_s.split("日落"))
date = []
for i in range(1, 8):
date_list = []
date_data = selector.xpath('//ul[@class="week"]/li[%d]/b/text()' % i)[
0
]
date_week = selector.xpath('//ul[@class="week"]/li[%d]/span/text()' % i)[
0
]
date_list.append(date_data)
date_list.append(date_week)
date.append(date_list)
weather = []
for i in range(1, 8):
weather_data = selector.xpath(
'//ul[@class="txt txt2"]/li[%d]/text()' % i
)
weather.append(weather_data)
temp = []
for i in range(1, 8):
temp_data = []
temp_high = selector.xpath(
'//div[@class="zxt_shuju"]/ul/li[%d]/span/text()' % i
)
temp_low = selector.xpath(
'//div[@class="zxt_shuju"]/ul/li[%d]/b/text()' % i
)
temp_data.append(temp_high)
temp_data.append(temp_low)
temp.append(temp_data)
wind = []
for i in range(1, 8):
wind_data = selector.xpath('//ul[@class="txt"]/li[%d]/text()' % i)
wind.append(wind_data)
weather_info = """
%s %s
************************************************
+ 温度:%s
+ 天气情况:%s
+ 温度范围:%s
+ %s
+ %s
+ %s
+ %s
+ %s
+ %s
+ %s
-----------------------------------------------------------------------
%s未来7天的天气
-----------------------------------------------------------------------
【%s %s:%s , 最高气温:%s , 最低气温:%s , %s】
【%s %s:%s , 最高气温:%s , 最低气温:%s , %s】
【%s %s:%s , 最高气温:%s , 最低气温:%s , %s】
【%s %s:%s , 最高气温:%s , 最低气温:%s , %s】
【%s %s:%s , 最高气温:%s , 最低气温:%s , %s】
【%s %s:%s , 最高气温:%s , 最低气温:%s , %s】
【%s %s:%s , 最高气温:%s , 最低气温:%s , %s】
-----------------------------------------------------------------------
""" % (
name,
date_rili,
now_temp,
now_w,
today_temp,
dampness,
now_wind,
now_sunshine,
air,
pm,
sun_richu,
sun_riluo,
name,
date,
date,
weather,
temp,
temp,
wind,
date,
date,
weather,
temp,
temp,
wind,
date,
date,
weather,
temp,
temp,
wind,
date,
date,
weather,
temp,
temp,
wind,
date,
date,
weather,
temp,
temp,
wind,
date,
date,
weather,
temp,
temp,
wind,
date,
date,
weather,
temp,
temp,
wind,
)
print(weather_info)
except Exception as e:
print(str(e))
today = date
city = ["chengdu"]
# import smtplib
# from email.mime.text import MIMEText
# mailto_list=''
# mail_host=""
# mail_user="****"
# mail_pass="*************"
# mail_postfix=""
weather_info_all = """"""
for i in city:
get_weather_info(i)
weather_info_all = weather_info_all + weather_info
# try:
# msg = MIMEText(weather_info_all)
# msg["Subject"] = today + "天气预报"
# msg["From"] = mail_user
# msg["To"] = mailto_list
# s = smtplib.SMTP_SSL(mail_host,465)
# s.login(mail_user, mail_pass)
# s.sendmail(mail_user, mailto_list, msg.as_string())
# s.quit()
# except Exception as e:
# print("Falied,%s"%e)
# 并没有什么
结果:(这有什么卵用,当然是树莓派自己搞一个三)
g1201314 发表于 2021-4-7 13:22
能不能搭建到腾讯云函数上呢
#添加了sever酱提醒功能,并对server酱文本进行了排版,会更加好看自行添加SCKEY,如需上传至腾讯云函数需要自行打包LXML依赖库一并上传,不然会报错。lxml依赖库请在centos下打包,win打包也会报错。
https://github.com/zeenwee/Baim-on-key/blob/master/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20210408094627.jpg
# -*- coding: utf-8 -*-
"""
learn python:tianqi
<通过天气网的接口,制作专属天气预报,可以对接树莓派哦,搞个屏幕>
:copyright: (c) 2019 by learn python.
:license: GPLv3, see LICENSE File for more details.
"""
#添加了sever酱提醒功能,自行添加SCKEY,如需上传至腾讯云函数需要自行打包LXML依赖库一并上传,不然会报错。lxml依赖库请在centos下打包,win打包也会报错。
import requests
from lxml import etree
requests.packages.urllib3.disable_warnings()
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0"
}
def getText(elem):
rc = []
for node in elem.itertext():
rc.append(node.strip())
return "".join(rc)
def get_weather_info(addr):
global weather_info, today, date
url = "https://www.tianqi.com/" + str(addr)
responese = requests.get(url, headers=headers, verify=False)
html = responese.text
selector = etree.HTML(html)
try:
name = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="name"]/h2/text()'
)
date_rili = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="week"]/text()'
)
now_temp = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="weather"]/p[@class="now"]/b/text()'
)
now_w = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="weather"]/span/b/text()'
)
today_temp = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="weather"]/span/text()'
)
dampness = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="shidu"]/b/text()'
)
now_wind = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="shidu"]/b/text()'
)
now_sunshine = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="shidu"]/b/text()'
)
air = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="kongqi"]/h5/text()'
)
pm = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="kongqi"]/h6/text()'
)
node = selector.xpath(
'//div[@class="weatherbox"]/div[@class="wrap1100"]/div[@class="left"]/dl[@class="weather_info"]/dd[@class="kongqi"]/span'
)
sun_s = getText(node)
sun_richu = sun_s.split("日落")
sun_riluo = "日落" + str(sun_s.split("日落"))
date = []
for i in range(1, 8):
date_list = []
date_data = selector.xpath('//ul[@class="week"]/li[%d]/b/text()' % i)[
0
]
date_week = selector.xpath('//ul[@class="week"]/li[%d]/span/text()' % i)[
0
]
date_list.append(date_data)
date_list.append(date_week)
date.append(date_list)
weather = []
for i in range(1, 8):
weather_data = selector.xpath(
'//ul[@class="txt txt2"]/li[%d]/text()' % i
)
weather.append(weather_data)
temp = []
for i in range(1, 8):
temp_data = []
temp_high = selector.xpath(
'//div[@class="zxt_shuju"]/ul/li[%d]/span/text()' % i
)
temp_low = selector.xpath(
'//div[@class="zxt_shuju"]/ul/li[%d]/b/text()' % i
)
temp_data.append(temp_high)
temp_data.append(temp_low)
temp.append(temp_data)
wind = []
for i in range(1, 8):
wind_data = selector.xpath('//ul[@class="txt"]/li[%d]/text()' % i)
wind.append(wind_data)
weather_info = """
%s %s
+ 温度:%s
+ 天气情况:%s
+ 温度范围:%s
+ %s
+ %s
+ %s
+ %s
+ %s
+ %s
+ %s
------------------------------
#%s未来7天的天气
------------------------------
日期 | 星期 | 天气状况 |最高气温℃ |最低气温℃ |风向
%s | %s | %s | %s | %s | %s
%s | %s | %s | %s | %s | %s
%s | %s | %s | %s | %s | %s
%s | %s | %s | %s | %s | %s
%s | %s | %s | %s | %s | %s
%s | %s | %s | %s | %s | %s
%s | %s | %s | %s | %s | %s
---------------------------------
""" % (
name,
date_rili,
now_temp,
now_w,
today_temp,
dampness,
now_wind,
now_sunshine,
air,
pm,
sun_richu,
sun_riluo,
name,
date,
date,
weather,
temp,
temp,
wind,
date,
date,
weather,
temp,
temp,
wind,
date,
date,
weather,
temp,
temp,
wind,
date,
date,
weather,
temp,
temp,
wind,
date,
date,
weather,
temp,
temp,
wind,
date,
date,
weather,
temp,
temp,
wind,
date,
date,
weather,
temp,
temp,
wind,
)
except Exception as e:
print(str(e))
today = date
city = ["shenzhen"]
# import smtplib
# from email.mime.text import MIMEText
# mailto_list=''
# mail_host=""
# mail_user="****"
# mail_pass="*************"
# mail_postfix=""
weather_info_all = """"""
for i in city:
get_weather_info(i)
weather_info_all = weather_info_all + weather_info
def main():
msg = weather_info_all
return msg
# msg["Subject"] = today + "天气预报"
# msg["From"] = mail_user
# msg["To"] = mailto_list
# s = smtplib.SMTP_SSL(mail_host,465)
# s.login(mail_user, mail_pass)
# s.sendmail(mail_user, mailto_list, msg.as_string())
# s.quit()
# except Exception as e:
# print("Falied,%s"%e)
# 并没有什么
def server_send():
server_url = "https://sctapi.ftqq.com/" + "SCKEY" + ".send"
data = {
'text': '来自打工人的每日天气预报~',
'desp': "-" + msg
}
requests.post(server_url, data=data)
def main_handler(event, context):
server_send()
楼主学习了,能不能封装一个接口呢? yzqhj 发表于 2020-11-23 11:27
楼主学习了,能不能封装一个接口呢?
没有考虑过呢 kuruite 发表于 2020-11-23 11:28
没有考虑过呢
怎么我把city换值后,报错date is not defined 好家伙,学习一下嘻嘻嘻 这么优秀的 我发现论坛好多人用Python 看来得拾起遗落的东西了 学习了,树莓派加个屏幕。 真的不错,学习一下 这个不错。python还是很强大。 {:1_919:}{:1_919:}{:1_919:}
进来学习,谢谢分享。
页:
[1]
2