最新国产好看的视频,伊人天堂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進行爬取,并通過正則的方式進行解析,最終以字典的形式返回結果。

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")
 
        # 結果生
        dic = {
            '城市': key,
            '更新時間': time,
            '天氣': weather,
            '溫度': weather_l + '-' + weather_h + '攝氏度',
            '風力': wind,
            '貼示': desc,
        }
        return dic

寫好了爬取天氣預報的代碼之后,下面就可以寫一個UI來和輸入/爬取的內容進行交互的,寫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)
 
        # 結果
        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)
 
        # 將結果顯示在窗口中的文本框中
        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')
 
    # 清空文本域中的內容
    def clean(self):
        self.result_text1.delete(0.0, END)
        self.result_text.delete(0.0, END)
 
    def run(self):
        self.window.mainloop()

運行結果如下:

四、完整代碼

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")
 
        # 結果生
        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)
 
        # 結果
        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)
 
        # 將結果顯示在窗口中的文本框中
        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')
 
    # 清空文本域中的內容
    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()

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

相關文章

最新評論

尼玛县| 正蓝旗| 平江县| 达州市| 黎川县| 思南县| 大同市| 杨浦区| 沙坪坝区| 临西县| 嘉荫县| 大洼县| 秦安县| 黄龙县| 苍山县| 图们市| 昌图县| 确山县| 广宗县| 溧阳市| 托克逊县| 淳化县| 古丈县| 镇赉县| 五大连池市| 彩票| 聂拉木县| 桂平市| 清涧县| 曲周县| 兰西县| 重庆市| 南木林县| 天门市| 津市市| 礼泉县| 博乐市| 鄂托克前旗| 柏乡县| 丹寨县| 泾川县|