這篇文章主要介紹python可視化爬蟲界面之天氣查詢的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比市中網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式市中網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋市中地區(qū)。費(fèi)用合理售后完善,10余年實(shí)體公司更值得信賴。執(zhí)行效果如下:
from tkinter import * import urllib.request import gzip import json from tkinter import messagebox root = Tk() def main(): # 輸入窗口 root.title('Python學(xué)習(xí)交流群:973783996') # 窗口標(biāo)題 Label(root, text='請輸入城市').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, 'Python學(xué)習(xí)交流群:973783996') # 設(shè)置默認(rèn)文本 # 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 + '天氣狀況') # 副窗口標(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)級'), (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()
以上是“python可視化爬蟲界面之天氣查詢的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享文章:python可視化爬蟲界面之天氣查詢的示例分析-創(chuàng)新互聯(lián)
文章起源:http://jinyejixie.com/article38/coecsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站設(shè)計(jì)、Google、微信公眾號、ChatGPT、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容