亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Python tkinter界面實現歷史天氣查詢的示例代碼

瀏覽:3日期:2022-07-13 13:07:18

一、實現效果

1. python代碼

import requestsfrom lxml import etreeimport reimport tkinter as tkfrom PIL import Image, ImageTkfrom xpinyin import Pinyindef get_image(file_nam, width, height): im = Image.open(file_nam).resize((width, height)) return ImageTk.PhotoImage(im)def spider(): headers = { ’user-agent’: ’Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24’, 'referer': 'https://lishi.tianqi.com/chengdu/index.html' } p = Pinyin() place = ’’.join(p.get_pinyin(b1.get()).split(’-’)) # 獲取地區文本框的輸入 變為拼音 # 處理用戶輸入的時間 # 規定三種格式都可以 2018/10/1 2018年10月1日 2018-10-1 date = b2.get() # 獲取時間文本框的輸入 if ’/’ in date: tm_list = date.split(’/’) elif ’-’ in date: tm_list = date.split(’-’) else: tm_list = re.findall(r’d+’, date) if int(tm_list[1]) < 10: # 1-9月 前面加 0 tm_list[1] = f’0{tm_list[1]}’ # 分析網頁規律 構造url # 直接訪問有該月所有天氣信息的頁面 提高查詢效率 url = f'https://lishi.tianqi.com/{place}/{’’.join(tm_list[:2])}.html' resp = requests.get(url, headers=headers) html = etree.HTML(resp.text) # xpath定位提取該日天氣信息 info = html.xpath(f’//ul[@class='thrui']/li[{int(tm_list[2])}]/div/text()’) # 輸出信息格式化一下 info1 = [’日期:’, ’最高氣溫:’, ’最低氣溫:’, ’天氣:’, ’風向:’] datas = [i + j for i, j in zip(info1, info)] info = ’n’.join(datas) t.insert(’insert’, ’ 查詢結果如下 nn’) t.insert(’insert’, info) print(info)win = tk.Tk()win.title(’全國各地歷史天氣查詢系統’)win.geometry(’500x500’)# 畫布 設置背景圖片canvas = tk.Canvas(win, height=500, width=500)im_root = get_image(’test.jpg’, width=500, height=500)canvas.create_image(250, 250, image=im_root)canvas.pack()# 單行文本L1 = tk.Label(win, bg=’yellow’, text='地區:', font=('SimHei', 12))L2 = tk.Label(win, bg=’yellow’, text='時間:', font=('SimHei', 12))L1.place(x=85, y=100)L2.place(x=85, y=150)# 單行文本框 可采集鍵盤輸入b1 = tk.Entry(win, font=('SimHei', 12), show=None, width=35)b2 = tk.Entry(win, font=('SimHei', 12), show=None, width=35)b1.place(x=140, y=100)b2.place(x=140, y=150)# 設置查詢按鈕a = tk.Button(win, bg=’red’, text='查詢', width=25, height=2, command=spider)a.place(x=160, y=200)# 設置多行文本框 寬 高 文本框中字體 選中文字時文字的顏色t = tk.Text(win, width=30, height=8, font=('SimHei', 18), selectforeground=’red’) # 顯示多行文本t.place(x=70, y=280)# 進入消息循環win.mainloop()

2. 運行效果

運行效果如下:

Python tkinter界面實現歷史天氣查詢的示例代碼

二、基本思路

導入用到的庫

import requestsfrom lxml import etreeimport reimport tkinter as tkfrom PIL import Image, ImageTkfrom xpinyin import Pinyin

1. 爬蟲部分

目標url:https://lishi.tianqi.com/

該網站提供了全國34個省、市所屬的2290個地區的歷史天氣預報查詢,數據來源于城市當天的天氣信息,可以查詢到歷史天氣氣溫,歷史風向,歷史風力等歷史天氣狀況。

Python tkinter界面實現歷史天氣查詢的示例代碼

Python tkinter界面實現歷史天氣查詢的示例代碼

分析網頁可以發現,某個地區、某個月的所有天氣數據的url為:https://lishi.tianqi.com/ + 地區名字的拼音 + ‘/’ + 年月.html。根據用戶輸入的地區和時間,進行字符串的處理,構造出url,用于request請求有該月所有天氣信息的頁面,獲取響應后Xpath定位提取用戶輸入的要查詢的日期的天氣信息,查詢結果顯示在tkinter界面。

爬蟲代碼如下:

def spider(): headers = { ’user-agent’: ’Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24’, 'referer': 'https://lishi.tianqi.com/chengdu/index.html' } p = Pinyin() place = ’’.join(p.get_pinyin(b1.get()).split(’-’)) # 獲取地區文本框的輸入 變為拼音 # 處理用戶輸入的時間 # 規定三種格式都可以 2018/10/1 2018年10月1日 2018-10-1 date = b2.get() # 獲取時間文本框的輸入 if ’/’ in date: tm_list = date.split(’/’) elif ’-’ in date: tm_list = date.split(’-’) else: tm_list = re.findall(r’d+’, date) if int(tm_list[1]) < 10: # 1-9月 前面加 0 tm_list[1] = f’0{tm_list[1]}’ # 分析網頁發現規律 構造url # 直接訪問有該月所有天氣信息的頁面 提高查詢效率 url = f'https://lishi.tianqi.com/{place}/{’’.join(tm_list[:2])}.html' resp = requests.get(url, headers=headers) html = etree.HTML(resp.text) # xpath定位提取該日天氣信息 info = html.xpath(f’//ul[@class='thrui']/li[{int(tm_list[2])}]/div/text()’) # 輸出信息格式化一下 info1 = [’日期:’, ’最高氣溫:’, ’最低氣溫:’, ’天氣:’, ’風向:’] datas = [i + j for i, j in zip(info1, info)] info = ’n’.join(datas) t.insert(’insert’, ’ 查詢結果如下 nn’) t.insert(’insert’, info) print(info)

2. tkinter界面

代碼如下:

def get_image(file_nam, width, height): im = Image.open(file_nam).resize((width, height)) return ImageTk.PhotoImage(im)win = tk.Tk()# 設置窗口title和大小win.title(’全國各地歷史天氣查詢系統’)win.geometry(’500x500’)# 畫布 設置背景圖片canvas = tk.Canvas(win, height=500, width=500)im_root = get_image(’test.jpg’, width=500, height=500)canvas.create_image(250, 250, image=im_root)canvas.pack()# 單行文本L1 = tk.Label(win, bg=’yellow’, text='地區:', font=('SimHei', 12))L2 = tk.Label(win, bg=’yellow’, text='時間:', font=('SimHei', 12))L1.place(x=85, y=100)L2.place(x=85, y=150)# 單行文本框 可采集鍵盤輸入b1 = tk.Entry(win, font=('SimHei', 12), show=None, width=35)b2 = tk.Entry(win, font=('SimHei', 12), show=None, width=35)b1.place(x=140, y=100)b2.place(x=140, y=150)# 設置查詢按鈕 點擊 調用爬蟲函數實現查詢a = tk.Button(win, bg=’red’, text='查詢', width=25, height=2, command=spider)a.place(x=160, y=200)# 設置多行文本框 寬 高 文本框中字體 選中文字時文字的顏色t = tk.Text(win, width=30, height=8, font=('SimHei', 18), selectforeground=’red’) # 顯示多行文本t.place(x=70, y=280)# 進入消息循環win.mainloop()

tkinter界面效果如下:

Python tkinter界面實現歷史天氣查詢的示例代碼

到此這篇關于Python tkinter界面實現歷史天氣查詢的示例代碼的文章就介紹到這了,更多相關Python tkinter歷史天氣查詢內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: a级毛片免费看 | 国产欧美精品午夜在线播放 | 日本色图视频 | 91最懂男人的午夜社区 | 国产一级第一级毛片 | 路线1路线二线路三国产 | 久久国产免费观看精品 | 日本精品久久久久久久 | 亚洲福利在线视频 | 午夜黄色 | 尤物网在线观看 | 一级女性全黄生活片看看 | 青草青视频在线观看 | 黄色网页免费 | 九九精品视频在线观看 | 国产欧美日韩在线播放 | 国产手机在线小视频免费观看 | 纯欧美一级毛片_免费 | 97青青草视频 | 精品亚洲大全 | 老司机深夜影院入口aaaa | 黄色在线观看www | 中文国产成人精品久久水 | 亚洲视频国产 | 男人你懂的在线观看视频 | 男人看片资源 | 黄色在线视频在线观看 | 亚洲精品亚洲人成在线观看麻豆 | 曰本黄色录像 | 一级全黄生活片 | 久久99精品国产免费观看 | 亚洲第一成年网 | 高h喷水荡肉爽文np肉色文 | 中文精品久久久久国产网站 | 亚洲国产日韩在线观频 | 九九草在线观看 | 欧美一区在线观看视频 | 欧美日韩国产另类一区二区三区 | 五月激激激综合网色播免费 | 国产成人影院 | 久久逼网 |