Python 爬虫实现天气查询(可视化界面版)
生活随笔
收集整理的這篇文章主要介紹了
Python 爬虫实现天气查询(可视化界面版)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
github項(xiàng)目地址:StarMan
Python 實(shí)現(xiàn)天氣查詢的程序早已完成,近日開學(xué)無課,昨晚心血來潮想做一個(gè)較為友好的界面版本,便匆忙行動(dòng)了起來。
在之前已有的程序的基礎(chǔ)上使用Tkinter 模塊實(shí)現(xiàn)GUI 并不是很難,但是在做的過程中《我的英雄學(xué)院》更新了,所以中途耽誤了,今天早上才做好。(~.~)
代碼的主體是爬蟲與Tkinter。
執(zhí)行程序后會(huì)先出現(xiàn)一個(gè)選擇城市的界面,這里需要輸入城市名。點(diǎn)擊確認(rèn)即會(huì)出現(xiàn)城市天氣狀況。
執(zhí)行效果如下:
?
以下為源代碼:
from tkinter import * import urllib.request import gzip import json from tkinter import messageboxroot = Tk()def main():#輸入窗口root.title('天氣查詢')#窗口標(biāo)題Label(root,text = '請(qǐng)輸入城市').grid(row=0,column=0)#設(shè)置標(biāo)簽并調(diào)整位置enter = Entry(root)#輸入框enter.grid(row = 0,column=1,padx = 20, pady = 20)#調(diào)整位置enter.delete(0,END)#清空輸入框enter.insert(0,'湘潭')#設(shè)置默認(rèn)文本#enter_text = enter.get()#獲取輸入框的內(nèi)容 running = 1def get_weather_data() :#獲取網(wǎng)站數(shù)據(jù)city_name = enter.get()#獲取輸入框的內(nèi)容url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'#網(wǎng)址1只需要輸入城市名,網(wǎng)址2需要輸入城市代碼#print(url1)weather_data = urllib.request.urlopen(url1).read()#讀取網(wǎng)頁數(shù)據(jù)weather_data = gzip.decompress(weather_data).decode('utf-8')#解壓網(wǎng)頁數(shù)據(jù)weather_dict = json.loads(weather_data)#將json數(shù)據(jù)轉(zhuǎn)換為dict數(shù)據(jù)if weather_dict.get('desc') == 'invilad-citykey':print(messagebox.askokcancel("xing","你輸入的城市名有誤,或者天氣中心未收錄你所在城市"))else:#print(messagebox.askokcancel('xing','bingguo')) show_data(weather_dict,city_name)def show_data(weather_dict,city_name):#顯示數(shù)據(jù)forecast = weather_dict.get('data').get('forecast')#獲取數(shù)據(jù)塊root1=Tk()#副窗口root1.geometry('650x280')#修改窗口大小root1.title(city_name + '天氣狀況')#副窗口標(biāo)題#設(shè)置日期列表for i in range(5):#將每一天的數(shù)據(jù)放入列表中LANGS = [(forecast[i].get('date'),'日期'),(forecast[i].get('fengxiang'),'風(fēng)向'),(str(forecast[i].get('fengji')),'風(fēng)級(jí)'),(forecast[i].get('high'),'最高溫'),(forecast[i].get('low'),'最低溫'),(forecast[i].get('type'),'天氣')]group = LabelFrame(root1,text = '天氣狀況',padx = 0,pady = 0)#框架group.pack(padx=11,pady=0,side = LEFT)#放置框架for lang, value in LANGS:#將數(shù)據(jù)放入框架中c = Label(group,text = value + ': ' + lang)c.pack(anchor = W)Label(root1,text = '今日' + weather_dict.get('data').get('ganmao'),fg = 'green').place(x=40,y=20,height=40)#溫馨提示Label(root1,text = "StarMan: 49star.com",fg = "green",bg = "yellow").place(x=10,y=255,width=125,height=20)#作者網(wǎng)站Button(root1,text = '確認(rèn)并退出',width=10,command = root1.quit).place(x=500,y=230,width = 80,height=40)#退出按鈕 root1.mainloop()#布置按鍵 Button(root, text = "確認(rèn)",width=10,command = get_weather_data)\.grid(row = 3, column=0,sticky = W, padx = 10, pady = 5)Button(root, text = '退出',width=10,command = root.quit)\.grid(row = 3, column = 1, sticky = E, padx = 10, pady = 5)if running==1:root.mainloop()if __name__ == '__main__':main()?
今天海賊王更新!!共勉!
轉(zhuǎn)載于:https://www.cnblogs.com/jxxclj/p/9572919.html
總結(jié)
以上是生活随笔為你收集整理的Python 爬虫实现天气查询(可视化界面版)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 绝对干货,教你4分钟插入1000万条数据
- 下一篇: 数学:FFT