最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實現(xiàn)天氣查詢軟件

 更新時間:2021年06月07日 14:55:07   作者:數(shù)據(jù)山谷  
今天給大家?guī)硪粋€不是那么實用的小工具-天氣查詢,文中詳細介紹了如何實用Python實現(xiàn)天氣查詢系統(tǒng),需要的朋友可以參考下

一、背景

某天下班淋雨成了落湯雞,發(fā)了個朋友圈感慨一下啊,然后......

夜深人靜之時,突然收到了來自學妹的Py文件,運行之后發(fā)現(xiàn)事情并不簡單(如下圖):

這是暗示我...下次出門給她帶把傘?不管那么多,作為一個程序猿,遇到程序先拆解一下。

二、工具

爬蟲:requests

解析:re

UI:tkinter

三、代碼解讀

想要做一個獲取天氣預報的小程序,第一步要做的就是能夠進行天氣預報的爬取,這里通過城市名稱匹配百度天氣的URL進行爬取,并通過正則的方式進行解析,最終以字典的形式返回結(jié)果。

class Weather(object):
    def __init__(self):
        pass
 
    def crawl(self, key):
 
        url = 'http://weathernew.pae.baidu.com/weathernew/pc?query=' + key + '天氣&srcid=4982&city_name=' + key + '&province_name=' + key
 
        # 設置請求頭
        headers = {
            'user-agent':
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
            'Referer': 'https://googleads.g.doubleclick.net/'
        }
        # 頁面HTML
        res = requests.get(url, headers=headers).text
        # 時間
        time = re.search(r'\{\"update_time\":\"(.+?)\"', res).group(1)
        # 天氣
        weather = re.search(r'\"weather\"\:\"(.+?)\"', res).group(1)
        weather = weather.encode('utf-8').decode("unicode-escape")
        # 氣溫
        weather_l = re.search(r'temperature_night\"\:\"(.+?)\"', res).group(1)
        weather_h = re.search(r'temperature_day\"\:\"(.+?)\"', res).group(1)
        # 風力
        wind_now = re.search(r'\"wind_power_day\"\:\"(.+?)\"', res).group(1)
        wind_now = wind_now.encode('utf-8').decode("unicode-escape")
        wind_name = re.search(r'\"wind_direction_day\"\:\"(.+?)\"',
                              res).group(1)
        wind_name = wind_name.encode('utf-8').decode("unicode-escape")
        wind = wind_name + wind_now
        # 貼示
        desc = re.search(r'\"desc\"\:\"(.+?)\"', res).group(1)
        desc = desc.encode('utf-8').decode("unicode-escape")
 
        # 結(jié)果生
        dic = {
            '城市': key,
            '更新時間': time,
            '天氣': weather,
            '溫度': weather_l + '-' + weather_h + '攝氏度',
            '風力': wind,
            '貼示': desc,
        }
        return dic

寫好了爬取天氣預報的代碼之后,下面就可以寫一個UI來和輸入/爬取的內(nèi)容進行交互的,寫UI的方式大同小異,代碼如下:

class Weather_UI(object):
    def __init__(self):
        self.window = Tk()
        self.weather = Weather()
 
        self.window.title(u'天氣預報')
        # 設置窗口大小和位置
        self.window.geometry('310x370')
        # 創(chuàng)建一個文本框
        self.result_text0 = Label(self.window, text=u'學長所在城市:\n要寫中文呦')
        self.result_text0.place(x=10, y=5, height=130)
        self.result_text0.bind('提示')
 
        self.result_text1 = Text(self.window, background='#ccc')
        self.result_text1.place(x=140, y=10, width=155, height=155)
        self.result_text1.bind("<Key-Return>", self.submit)
 
        # 創(chuàng)建一個按鈕
        # 為按鈕添加事件
        self.submit_btn = Button(self.window,
                                 text=u'獲取天氣',
                                 command=self.submit)
        self.submit_btn.place(x=170, y=165, width=70, height=25)
        self.submit_btn2 = Button(self.window, text=u'清空', command=self.clean)
        self.submit_btn2.place(x=250, y=165, width=35, height=25)
 
        # 標題
        self.title_label = Label(self.window, text=u'今日天氣:')
        self.title_label.place(x=10, y=165)
 
        # 結(jié)果
        self.result_text = Text(self.window, background='#ccc')
        self.result_text.place(x=10, y=190, width=285, height=165)
 
    def submit(self):
        # 從輸入框獲取用戶輸入的值
        content = self.result_text1.get(0.0, END).strip().replace("\n", " ")
 
        # 把城市信息傳到爬蟲函數(shù)中
        result = self.weather.crawl(content)
 
        # 將結(jié)果顯示在窗口中的文本框中
        for k, v in result.items():
            self.result_text.insert(END, k + ':' + v)
            self.result_text.insert(END, '\n')
            self.result_text.insert(END, '\n')
 
    # 清空文本域中的內(nèi)容
    def clean(self):
        self.result_text1.delete(0.0, END)
        self.result_text.delete(0.0, END)
 
    def run(self):
        self.window.mainloop()

運行結(jié)果如下:

四、完整代碼

import json
import requests
import re
import tkinter as Tk
from tkinter import Tk, Button, Entry, Label, Text, END
 
 
class Weather(object):
    def __init__(self):
        pass
 
    def crawl(self, key):
 
        url = 'http://weathernew.pae.baidu.com/weathernew/pc?query=' + key + '天氣&srcid=4982&city_name=' + key + '&province_name=' + key
 
        # 設置請求頭
        headers = {
            'user-agent':
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
            'Referer': 'https://googleads.g.doubleclick.net/'
        }
        # 頁面HTML
        res = requests.get(url, headers=headers).text
        # 時間
        time = re.search(r'\{\"update_time\":\"(.+?)\"', res).group(1)
        # 天氣
        weather = re.search(r'\"weather\"\:\"(.+?)\"', res).group(1)
        weather = weather.encode('utf-8').decode("unicode-escape")
        # 氣溫
        weather_l = re.search(r'temperature_night\"\:\"(.+?)\"', res).group(1)
        weather_h = re.search(r'temperature_day\"\:\"(.+?)\"', res).group(1)
        # 風力
        wind_now = re.search(r'\"wind_power_day\"\:\"(.+?)\"', res).group(1)
        wind_now = wind_now.encode('utf-8').decode("unicode-escape")
        wind_name = re.search(r'\"wind_direction_day\"\:\"(.+?)\"',
                              res).group(1)
        wind_name = wind_name.encode('utf-8').decode("unicode-escape")
        wind = wind_name + wind_now
        # 貼示
        desc = re.search(r'\"desc\"\:\"(.+?)\"', res).group(1)
        desc = desc.encode('utf-8').decode("unicode-escape")
 
        # 結(jié)果生
        dic = {
            '城市': key,
            '更新時間': time,
            '天氣': weather,
            '溫度': weather_l + '-' + weather_h + '攝氏度',
            '風力': wind,
            '貼示': desc,
        }
        return dic
 
 
class Weather_UI(object):
    def __init__(self):
        self.window = Tk()
        self.weather = Weather()
 
        self.window.title(u'天氣預報')
        # 設置窗口大小和位置
        self.window.geometry('310x370')
        # 創(chuàng)建一個文本框
        self.result_text0 = Label(self.window, text=u'學長所在城市:\n要寫中文呦')
        self.result_text0.place(x=10, y=5, height=130)
        self.result_text0.bind('提示')
 
        self.result_text1 = Text(self.window, background='#ccc')
        self.result_text1.place(x=140, y=10, width=155, height=155)
        self.result_text1.bind("<Key-Return>", self.submit)
 
        # 創(chuàng)建一個按鈕
        # 為按鈕添加事件
        self.submit_btn = Button(self.window,
                                 text=u'獲取天氣',
                                 command=self.submit)
        self.submit_btn.place(x=170, y=165, width=70, height=25)
        self.submit_btn2 = Button(self.window, text=u'清空', command=self.clean)
        self.submit_btn2.place(x=250, y=165, width=35, height=25)
 
        # 標題
        self.title_label = Label(self.window, text=u'今日天氣:')
        self.title_label.place(x=10, y=165)
 
        # 結(jié)果
        self.result_text = Text(self.window, background='#ccc')
        self.result_text.place(x=10, y=190, width=285, height=165)
 
    def submit(self):
        # 從輸入框獲取用戶輸入的值
        content = self.result_text1.get(0.0, END).strip().replace("\n", " ")
 
        # 把城市信息傳到爬蟲函數(shù)中
        result = self.weather.crawl(content)
 
        # 將結(jié)果顯示在窗口中的文本框中
        for k, v in result.items():
            self.result_text.insert(END, k + ':' + v)
            self.result_text.insert(END, '\n')
            self.result_text.insert(END, '\n')
 
    # 清空文本域中的內(nèi)容
    def clean(self):
        self.result_text1.delete(0.0, END)
        self.result_text.delete(0.0, END)
 
    def run(self):
        self.window.mainloop()
 
 
A = Weather_UI()
A.run()

到此這篇關(guān)于Python實現(xiàn)天氣查詢系統(tǒng)的文章就介紹到這了,更多相關(guān)Python天氣查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

唐河县| 如东县| 宝山区| 禹城市| 石门县| 龙川县| 巩义市| 宿松县| 德清县| 平陆县| 吉木乃县| 泽普县| 金川县| 汉川市| 郴州市| 永泰县| 新疆| 元朗区| 鄂州市| 望都县| 吉隆县| 湖口县| 辽阳县| 东兰县| 娄烦县| 荣昌县| 济南市| 蒙阴县| 普兰县| 绥化市| 汕尾市| 白银市| 通州市| 湖南省| 潼关县| 阿城市| 平度市| 全州县| 遵义市| 北川| 富蕴县|