Python實現(xiàn)天氣查詢軟件
一、背景
某天下班淋雨成了落湯雞,發(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()
到此這篇關于Python實現(xiàn)天氣查詢系統(tǒng)的文章就介紹到這了,更多相關Python天氣查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決pyinstaller打包發(fā)布后的exe文件打開控制臺閃退的問題
今天小編就為大家分享一篇解決pyinstaller打包發(fā)布后的exe文件打開控制臺閃退的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06Python?pandas庫中isnull函數(shù)使用方法
這篇文章主要介紹了Python?pandas庫中isnull函數(shù)使用方法,python的pandas庫中有?個?分便利的isnull()函數(shù),它可以?來判斷缺失值,具體介紹需要的小伙伴可以參考一下2022-06-06Python中Pandas庫提供的函數(shù)pd.DataFrame的基本用法
pandas庫中的pd.DataFrame()函數(shù)用于創(chuàng)建一個DataFrame對象,它是一個二維表格數(shù)據(jù)結(jié)構,每列可以是不同的數(shù)據(jù)類型(數(shù)值、字符串、布爾值等),下面這篇文章主要給大家介紹了關于Python中Pandas庫提供的函數(shù)pd.DataFrame的基本用法,需要的朋友可以參考下2024-03-03Python中exit、return、sys.exit()等使用實例和區(qū)別
這篇文章主要介紹了Python中exit、return、sys.exit()等使用實例和區(qū)別,本文是一個實際項目中的總結(jié),需要的朋友可以參考下2015-05-05