python可視化爬蟲界面之天氣查詢
更新時間:2019年07月03日 11:52:30 作者:嗨學編程
這篇文章主要介紹了python可視化爬蟲界面之天氣查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
執(zhí)行效果如下:


from tkinter import *
import urllib.request
import gzip
import json
from tkinter import messagebox
root = Tk()
def main():
# 輸入窗口
root.title('Python學習交流群:973783996') # 窗口標題
Label(root, text='請輸入城市').grid(row=0, column=0) # 設(shè)置標簽并調(diào)整位置
enter = Entry(root) # 輸入框
enter.grid(row=0, column=1, padx=20, pady=20) # 調(diào)整位置
enter.delete(0, END) # 清空輸入框
enter.insert(0, 'Python學習交流群:973783996') # 設(shè)置默認文本
# enter_text = enter.get()#獲取輸入框的內(nèi)容
running = 1
def 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 + '天氣狀況') # 副窗口標題
# 設(shè)置日期列表
for i in range(5): # 將每一天的數(shù)據(jù)放入列表中
LANGS = [(forecast[i].get('date'), '日期'),
(forecast[i].get('fengxiang'), '風向'),
(str(forecast[i].get('fengji')), '風級'),
(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='確認并退出', width=10, command=root1.quit).place(x=500, y=230, width=80, height=40) # 退出按鈕
root1.mainloop()
# 布置按鍵
Button(root, text="確認", 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()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python實現(xiàn)多進程按序號批量修改文件名的方法示例
這篇文章主要介紹了python實現(xiàn)多進程按序號批量修改文件名的方法,涉及Python多進程與文件相關(guān)操作技巧,需要的朋友可以參考下2019-12-12
Python中線程threading.Thread的使用詳解
python的thread模塊是比較底層的模塊,python的threading模塊是對thread做了一些包裝的,可以更加方便的被使用。本文將為大家詳細介紹一下python中的線程threading.Thread()的使用,需要的可以參考一下2022-07-07
Python中執(zhí)行JavaScript實現(xiàn)數(shù)據(jù)抓取的多種方法
JavaScript是一門強大的腳本語言,廣泛應(yīng)用于網(wǎng)頁前端開發(fā)、構(gòu)建交互式用戶界面以及處理各種客戶端端任務(wù),有時可能需要在Python環(huán)境中執(zhí)行JavaScript代碼,本文將介紹多種方法,幫助你在Python中執(zhí)行 JavaScript代碼,并提供詳盡的示例代碼,使你能夠輕松掌握這一技能2023-11-11
python3+PyQt5實現(xiàn)自定義窗口部件Counters
這篇文章主要為大家詳細介紹了python3+PyQt5實現(xiàn)自定義窗口部件,Counters自定窗口部件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
Python使用pyglet庫完整實現(xiàn)漢諾塔游戲流程詳解
這篇文章主要介紹了Python使用pyglet庫完整實現(xiàn)漢諾塔游戲流程,漢諾塔問題是一個遞歸問題,也可以使用非遞歸法來解決,這個問題不僅是一個數(shù)學和邏輯問題,也是一個很好的教學工具,可以用來教授遞歸、算法和邏輯思考等概念,需要的朋友可以參考下2007-02-02

