Python實(shí)現(xiàn)天氣查詢軟件
一、背景
某天下班淋雨成了落湯雞,發(fā)了個(gè)朋友圈感慨一下啊,然后......
夜深人靜之時(shí),突然收到了來自學(xué)妹的Py文件,運(yùn)行之后發(fā)現(xiàn)事情并不簡單(如下圖):
這是暗示我...下次出門給她帶把傘?不管那么多,作為一個(gè)程序猿,遇到程序先拆解一下。
二、工具
爬蟲:requests
解析:re
UI:tkinter
三、代碼解讀
想要做一個(gè)獲取天氣預(yù)報(bào)的小程序,第一步要做的就是能夠進(jìn)行天氣預(yù)報(bào)的爬取,這里通過城市名稱匹配百度天氣的URL進(jìn)行爬取,并通過正則的方式進(jìn)行解析,最終以字典的形式返回結(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 # 設(shè)置請求頭 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 # 時(shí)間 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) # 風(fēng)力 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, '更新時(shí)間': time, '天氣': weather, '溫度': weather_l + '-' + weather_h + '攝氏度', '風(fēng)力': wind, '貼示': desc, } return dic
寫好了爬取天氣預(yù)報(bào)的代碼之后,下面就可以寫一個(gè)UI來和輸入/爬取的內(nèi)容進(jìn)行交互的,寫UI的方式大同小異,代碼如下:
class Weather_UI(object): def __init__(self): self.window = Tk() self.weather = Weather() self.window.title(u'天氣預(yù)報(bào)') # 設(shè)置窗口大小和位置 self.window.geometry('310x370') # 創(chuàng)建一個(gè)文本框 self.result_text0 = Label(self.window, text=u'學(xué)長所在城市:\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)建一個(gè)按鈕 # 為按鈕添加事件 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) # 標(biāo)題 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()
運(yùn)行結(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 # 設(shè)置請求頭 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 # 時(shí)間 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) # 風(fēng)力 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, '更新時(shí)間': time, '天氣': weather, '溫度': weather_l + '-' + weather_h + '攝氏度', '風(fēng)力': wind, '貼示': desc, } return dic class Weather_UI(object): def __init__(self): self.window = Tk() self.weather = Weather() self.window.title(u'天氣預(yù)報(bào)') # 設(shè)置窗口大小和位置 self.window.geometry('310x370') # 創(chuàng)建一個(gè)文本框 self.result_text0 = Label(self.window, text=u'學(xué)長所在城市:\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)建一個(gè)按鈕 # 為按鈕添加事件 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) # 標(biāo)題 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實(shí)現(xiàn)天氣查詢系統(tǒng)的文章就介紹到這了,更多相關(guān)Python天氣查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python使用matplotlib繪制余弦的散點(diǎn)圖示例
這篇文章主要介紹了Python使用matplotlib繪制余弦的散點(diǎn)圖,涉及Python操作matplotlib的基本技巧與散點(diǎn)的設(shè)置方法,需要的朋友可以參考下2018-03-03解決pyinstaller打包發(fā)布后的exe文件打開控制臺閃退的問題
今天小編就為大家分享一篇解決pyinstaller打包發(fā)布后的exe文件打開控制臺閃退的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06python游戲?qū)崙?zhàn)項(xiàng)目之智能五子棋
下五子棋嗎?信不信我讓你幾步你也贏不了?本篇為你帶來用python編寫的五子棋小游戲,文中給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值2021-09-09Python?pandas庫中isnull函數(shù)使用方法
這篇文章主要介紹了Python?pandas庫中isnull函數(shù)使用方法,python的pandas庫中有?個(gè)?分便利的isnull()函數(shù),它可以?來判斷缺失值,具體介紹需要的小伙伴可以參考一下2022-06-06Python中Pandas庫提供的函數(shù)pd.DataFrame的基本用法
pandas庫中的pd.DataFrame()函數(shù)用于創(chuàng)建一個(gè)DataFrame對象,它是一個(gè)二維表格數(shù)據(jù)結(jié)構(gòu),每列可以是不同的數(shù)據(jù)類型(數(shù)值、字符串、布爾值等),下面這篇文章主要給大家介紹了關(guān)于Python中Pandas庫提供的函數(shù)pd.DataFrame的基本用法,需要的朋友可以參考下2024-03-03Python中exit、return、sys.exit()等使用實(shí)例和區(qū)別
這篇文章主要介紹了Python中exit、return、sys.exit()等使用實(shí)例和區(qū)別,本文是一個(gè)實(shí)際項(xiàng)目中的總結(jié),需要的朋友可以參考下2015-05-05