吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3487|回复: 16
收起左侧

[Python 转载] 通过天气网制作专属天气预报

  [复制链接]
kuruite 发表于 2020-11-23 11:24
本帖最后由 kuruite 于 2020-11-23 11:26 编辑

[Python] 纯文本查看 复制代码
# -*- 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"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="name"][1]/h2[1]/text()'
        )[0]
        date_rili = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="week"]/text()'
        )[0]
        now_temp = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="weather"][1]/p[@class="now"][1]/b[1]/text()'
        )[0]
        now_w = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="weather"][1]/span[1]/b[1]/text()'
        )[0]
        today_temp = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="weather"][1]/span[1]/text()'
        )[0]
        dampness = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="shidu"][1]/b[1]/text()'
        )[0]
        now_wind = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="shidu"][1]/b[2]/text()'
        )[0]
        now_sunshine = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="shidu"][1]/b[3]/text()'
        )[0]
        air = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="kongqi"][1]/h5[1]/text()'
        )[0]
        pm = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="kongqi"][1]/h6[1]/text()'
        )[0]
        node = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="kongqi"][1]/span[1]'
        )[0]
        sun_s = getText(node)
        sun_richu = sun_s.split("日落")[0]
        sun_riluo = "日落" + str(sun_s.split("日落")[1])
        date = []
        for i in range(1, 8):
            date_list = []
            date_data = selector.xpath('//ul[@class="week"][1]/li[%d]/b[1]/text()' % i)[
                0
            ]
            date_week = selector.xpath('//ul[@class="week"][1]/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"][1]/li[%d]/text()' % i
            )[0]
            weather.append(weather_data)
        temp = []
        for i in range(1, 8):
            temp_data = []
            temp_high = selector.xpath(
                '//div[@class="zxt_shuju"][1]/ul[1]/li[%d]/span[1]/text()' % i
            )[0]
            temp_low = selector.xpath(
                '//div[@class="zxt_shuju"][1]/ul[1]/li[%d]/b[1]/text()' % i
            )[0]
            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"][1]/li[%d]/text()' % i)[0]
            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[0][0],
            date[0][1],
            weather[0],
            temp[0][0],
            temp[0][1],
            wind[0],
            date[1][0],
            date[1][1],
            weather[1],
            temp[1][0],
            temp[1][1],
            wind[1],
            date[2][0],
            date[2][1],
            weather[2],
            temp[2][0],
            temp[2][1],
            wind[2],
            date[3][0],
            date[3][1],
            weather[3],
            temp[3][0],
            temp[3][1],
            wind[3],
            date[4][0],
            date[4][1],
            weather[4],
            temp[4][0],
            temp[4][1],
            wind[4],
            date[5][0],
            date[5][1],
            weather[5],
            temp[5][0],
            temp[5][1],
            wind[5],
            date[6][0],
            date[6][1],
            weather[6],
            temp[6][0],
            temp[6][1],
            wind[6],
        )
        print(weather_info)
    except Exception as e:
        print(str(e))
    today = date[0][0]


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)
    # 并没有什么



结果:(这有什么卵用,当然是树莓派自己搞一个三)
image.png

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

唯唯子 发表于 2021-4-8 09:49
g1201314 发表于 2021-4-7 13:22
能不能搭建到腾讯云函数上呢

#添加了sever酱提醒功能,并对server酱文本进行了排版,会更加好看自行添加SCKEY,如需上传至腾讯云函数需要自行打包LXML依赖库一并上传,不然会报错。lxml依赖库请在centos下打包,win打包也会报错。

[Python] 纯文本查看 复制代码
# -*- 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"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="name"][1]/h2[1]/text()'
        )[0]
        date_rili = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="week"]/text()'
        )[0]
        now_temp = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="weather"][1]/p[@class="now"][1]/b[1]/text()'
        )[0]
        now_w = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="weather"][1]/span[1]/b[1]/text()'
        )[0]
        today_temp = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="weather"][1]/span[1]/text()'
        )[0]
        dampness = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="shidu"][1]/b[1]/text()'
        )[0]
        now_wind = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="shidu"][1]/b[2]/text()'
        )[0]
        now_sunshine = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="shidu"][1]/b[3]/text()'
        )[0]
        air = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="kongqi"][1]/h5[1]/text()'
        )[0]
        pm = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="kongqi"][1]/h6[1]/text()'
        )[0]
        node = selector.xpath(
            '//div[@class="weatherbox"][1]/div[@class="wrap1100"][1]/div[@class="left"][1]/dl[@class="weather_info"][1]/dd[@class="kongqi"][1]/span[1]'
        )[0]
        sun_s = getText(node)
        sun_richu = sun_s.split("日落")[0]
        sun_riluo = "日落" + str(sun_s.split("日落")[1])
        date = []
        for i in range(1, 8):
            date_list = []
            date_data = selector.xpath('//ul[@class="week"][1]/li[%d]/b[1]/text()' % i)[
                0
            ]
            date_week = selector.xpath('//ul[@class="week"][1]/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"][1]/li[%d]/text()' % i
            )[0]
            weather.append(weather_data)
        temp = []
        for i in range(1, 8):
            temp_data = []
            temp_high = selector.xpath(
                '//div[@class="zxt_shuju"][1]/ul[1]/li[%d]/span[1]/text()' % i
            )[0]
            temp_low = selector.xpath(
                '//div[@class="zxt_shuju"][1]/ul[1]/li[%d]/b[1]/text()' % i
            )[0]
            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"][1]/li[%d]/text()' % i)[0]
            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[0][0],
            date[0][1],
            weather[0],
            temp[0][0],
            temp[0][1],
            wind[0],
            date[1][0],
            date[1][1],
            weather[1],
            temp[1][0],
            temp[1][1],
            wind[1],
            date[2][0],
            date[2][1],
            weather[2],
            temp[2][0],
            temp[2][1],
            wind[2],
            date[3][0],
            date[3][1],
            weather[3],
            temp[3][0],
            temp[3][1],
            wind[3],
            date[4][0],
            date[4][1],
            weather[4],
            temp[4][0],
            temp[4][1],
            wind[4],
            date[5][0],
            date[5][1],
            weather[5],
            temp[5][0],
            temp[5][1],
            wind[5],
            date[6][0],
            date[6][1],
            weather[6],
            temp[6][0],
            temp[6][1],
            wind[6],
        )

    except Exception as e:
        print(str(e))
    today = date[0][0]


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
yzqhj 发表于 2020-11-23 11:43

怎么我把city换值后,报错date is not defined
EphraimWin 发表于 2020-11-23 11:55
好家伙,学习一下  嘻嘻嘻
QingYi. 发表于 2020-11-23 12:20
这么优秀的 我发现论坛好多人用Python 看来得拾起遗落的东西了
xudabenshi 发表于 2020-11-23 12:28
学习了,树莓派加个屏幕。
gylgb 发表于 2020-11-23 13:00
真的不错,学习一下
五月何欢 发表于 2020-11-23 16:11
这个不错。python还是很强大。
hshcompass 发表于 2020-11-23 18:05

进来学习,谢谢分享。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 17:46

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表