国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python tkinter界面實(shí)現(xiàn)歷史天氣查詢的示例代碼

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

一、實(shí)現(xiàn)效果

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(’-’)) # 獲取地區(qū)文本框的輸入 變?yōu)槠匆? # 處理用戶輸入的時(shí)間 # 規(guī)定三種格式都可以 2018/10/1 2018年10月1日 2018-10-1 date = b2.get() # 獲取時(shí)間文本框的輸入 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]}’ # 分析網(wǎng)頁(yè)規(guī)律 構(gòu)造url # 直接訪問(wèn)有該月所有天氣信息的頁(yè)面 提高查詢效率 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 = [’日期:’, ’最高氣溫:’, ’最低氣溫:’, ’天氣:’, ’風(fēng)向:’] datas = [i + j for i, j in zip(info1, info)] info = ’n’.join(datas) t.insert(’insert’, ’ 查詢結(jié)果如下 nn’) t.insert(’insert’, info) print(info)win = tk.Tk()win.title(’全國(guó)各地歷史天氣查詢系統(tǒng)’)win.geometry(’500x500’)# 畫布 設(shè)置背景圖片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='地區(qū):', font=('SimHei', 12))L2 = tk.Label(win, bg=’yellow’, text='時(shí)間:', 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)# 設(shè)置查詢按鈕a = tk.Button(win, bg=’red’, text='查詢', width=25, height=2, command=spider)a.place(x=160, y=200)# 設(shè)置多行文本框 寬 高 文本框中字體 選中文字時(shí)文字的顏色t = tk.Text(win, width=30, height=8, font=('SimHei', 18), selectforeground=’red’) # 顯示多行文本t.place(x=70, y=280)# 進(jìn)入消息循環(huán)win.mainloop()

2. 運(yùn)行效果

運(yùn)行效果如下:

Python tkinter界面實(shí)現(xiàn)歷史天氣查詢的示例代碼

二、基本思路

導(dǎo)入用到的庫(kù)

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

1. 爬蟲部分

目標(biāo)url:https://lishi.tianqi.com/

該網(wǎng)站提供了全國(guó)34個(gè)省、市所屬的2290個(gè)地區(qū)的歷史天氣預(yù)報(bào)查詢,數(shù)據(jù)來(lái)源于城市當(dāng)天的天氣信息,可以查詢到歷史天氣氣溫,歷史風(fēng)向,歷史風(fēng)力等歷史天氣狀況。

Python tkinter界面實(shí)現(xiàn)歷史天氣查詢的示例代碼

Python tkinter界面實(shí)現(xiàn)歷史天氣查詢的示例代碼

分析網(wǎng)頁(yè)可以發(fā)現(xiàn),某個(gè)地區(qū)、某個(gè)月的所有天氣數(shù)據(jù)的url為:https://lishi.tianqi.com/ + 地區(qū)名字的拼音 + ‘/’ + 年月.html。根據(jù)用戶輸入的地區(qū)和時(shí)間,進(jìn)行字符串的處理,構(gòu)造出url,用于request請(qǐng)求有該月所有天氣信息的頁(yè)面,獲取響應(yīng)后Xpath定位提取用戶輸入的要查詢的日期的天氣信息,查詢結(jié)果顯示在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(’-’)) # 獲取地區(qū)文本框的輸入 變?yōu)槠匆? # 處理用戶輸入的時(shí)間 # 規(guī)定三種格式都可以 2018/10/1 2018年10月1日 2018-10-1 date = b2.get() # 獲取時(shí)間文本框的輸入 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]}’ # 分析網(wǎng)頁(yè)發(fā)現(xiàn)規(guī)律 構(gòu)造url # 直接訪問(wèn)有該月所有天氣信息的頁(yè)面 提高查詢效率 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 = [’日期:’, ’最高氣溫:’, ’最低氣溫:’, ’天氣:’, ’風(fēng)向:’] datas = [i + j for i, j in zip(info1, info)] info = ’n’.join(datas) t.insert(’insert’, ’ 查詢結(jié)果如下 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()# 設(shè)置窗口title和大小win.title(’全國(guó)各地歷史天氣查詢系統(tǒng)’)win.geometry(’500x500’)# 畫布 設(shè)置背景圖片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='地區(qū):', font=('SimHei', 12))L2 = tk.Label(win, bg=’yellow’, text='時(shí)間:', 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)# 設(shè)置查詢按鈕 點(diǎn)擊 調(diào)用爬蟲函數(shù)實(shí)現(xiàn)查詢a = tk.Button(win, bg=’red’, text='查詢', width=25, height=2, command=spider)a.place(x=160, y=200)# 設(shè)置多行文本框 寬 高 文本框中字體 選中文字時(shí)文字的顏色t = tk.Text(win, width=30, height=8, font=('SimHei', 18), selectforeground=’red’) # 顯示多行文本t.place(x=70, y=280)# 進(jìn)入消息循環(huán)win.mainloop()

tkinter界面效果如下:

Python tkinter界面實(shí)現(xiàn)歷史天氣查詢的示例代碼

到此這篇關(guān)于Python tkinter界面實(shí)現(xiàn)歷史天氣查詢的示例代碼的文章就介紹到這了,更多相關(guān)Python tkinter歷史天氣查詢內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 综合激情网站 | aaa级毛片 | 国产色手机在线观看播放 | 一级做a爰性色毛片 | 久久免费毛片 | 日本三区视频 | 免费精品一区二区三区在线观看 | 国产真实乱子伦xxxxchina | 欧美一区二区精品 | 欧美成人在线免费观看 | 黄色美女在线观看 | 亚洲国产欧美在线人成aaa | 免费99视频有精品视频高清 | 国产乱码精品一区二区三区四川人 | 国产一区2区 | 亚洲欧美一区二区三区四区 | 在线成人a毛片免费播放 | 亚洲免费在线 | 韩国免费网站成人 | 久久久综合视频 | 真人毛片免费全部播放完整 | 日韩精品久久久毛片一区二区 | 99久久精品免费观看国产 | 色综合在| 欧美成人午夜视频免看 | 久久国产99 | 欧美成人鲁丝片在线观看 | 欧美a在线 | 普通话对白国产精品一级毛片 | 欧美日韩精品一区二区三区 | 国产呦系列呦交 | 日本免费人成黄页在线观看视频 | 久久久精品久久久久三级 | 亚洲精品一区二区三区在线播放 | 九九亚洲视频 | 97超在线 | 亚洲三级在线观看 | 一区二区网站在线观看 | 北条麻妃在线一区二区 | 亚洲在线视频播放 | 欧美高清一区二区三区欧美 |