一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python tkinter界面實現(xiàn)歷史天氣查詢的示例代碼

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

2020-08-24 00:05葉庭云 Python

這篇文章主要介紹了Python tkinter界面實現(xiàn)歷史天氣查詢的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

一、實現(xiàn)效果

1. python代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import requests
from lxml import etree
import re
import tkinter as tk
from PIL import Image, ImageTk
from xpinyin import Pinyin
 
 
def 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)槠匆?/code>
  # 處理用戶輸入的時間
  # 規(guī)定三種格式都可以 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]}'
  # 分析網(wǎng)頁規(guī)律 構(gòu)造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', '    查詢結(jié)果如下    \n\n')
  t.insert('insert', info)
  print(info)
 
 
win = tk.Tk()
win.title('全國各地歷史天氣查詢系統(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="時間:", 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è)置多行文本框 寬 高 文本框中字體 選中文字時文字的顏色
t = tk.Text(win, width=30, height=8, font=("SimHei", 18), selectforeground='red') # 顯示多行文本
t.place(x=70, y=280)
 
# 進入消息循環(huán)
win.mainloop()

2. 運行效果

運行效果如下:

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

二、基本思路

導入用到的庫

?
1
2
3
4
5
6
import requests
from lxml import etree
import re
import tkinter as tk
from PIL import Image, ImageTk
from xpinyin import Pinyin

1. 爬蟲部分

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

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

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

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

分析網(wǎng)頁可以發(fā)現(xiàn),某個地區(qū)、某個月的所有天氣數(shù)據(jù)的url為:https://lishi.tianqi.com/ + 地區(qū)名字的拼音 + ‘/' + 年月.html。
根據(jù)用戶輸入的地區(qū)和時間,進行字符串的處理,構(gòu)造出url,用于request請求有該月所有天氣信息的頁面,獲取響應后Xpath定位提取用戶輸入的要查詢的日期的天氣信息,查詢結(jié)果顯示在tkinter界面。

爬蟲代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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)槠匆?/code>
  # 處理用戶輸入的時間
  # 規(guī)定三種格式都可以 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]}'
  # 分析網(wǎng)頁發(fā)現(xiàn)規(guī)律  構(gòu)造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', '    查詢結(jié)果如下    \n\n')
  t.insert('insert', info)
  print(info)

2. tkinter界面

代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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('全國各地歷史天氣查詢系統(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="時間:", 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ào)用爬蟲函數(shù)實現(xiàn)查詢
a = tk.Button(win, bg='red', text="查詢", width=25, height=2, command=spider)
a.place(x=160, y=200)
 
# 設(shè)置多行文本框 寬 高 文本框中字體 選中文字時文字的顏色
t = tk.Text(win, width=30, height=8, font=("SimHei", 18), selectforeground='red') # 顯示多行文本
t.place(x=70, y=280)
 
# 進入消息循環(huán)
win.mainloop()

tkinter界面效果如下:

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

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

原文鏈接:https://blog.csdn.net/fyfugoyfa/article/details/108104202

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 涩涩屋在线播放 | 91桃色视频在线观看 | 毛片网站观看 | 日出水了特别黄的视频 | 操女b| 国产综合欧美日韩视频一区 | 99这里精品 | 按摩椅play啊太快了h | 成人啪啪漫画全文阅读 | 操碰91| 韩国最新三级网站在线播放 | 亚洲男人第一天堂 | 污软件在线观看 | 欧美日本一区视频免费 | 国产良家| 5月色婷婷 | 高清不卡日本v在线二区 | 99热成人精品免费久久 | 好深快点再快点好爽视频 | 91精品天美精东蜜桃传媒免费 | 午夜影院在线免费观看 | 午夜国产 | 和日本免费不卡在线v | 俄罗斯毛片免费大全 | 亚洲六月丁香六月婷婷色伊人 | 调教扩张宫颈女人惨叫 | 国产精品女主播自在线拍 | 国产精品久久久久久搜索 | 高清欧美不卡一区二区三区 | 精品亚洲综合久久中文字幕 | 国产成人亚洲综合91精品555 | 免费一级欧美片在线观免看 | 武侠古典久久亚洲精品 | 私人chinese beauty| 男人在女人下面狂躁 | 2023毛片| 午夜精品网站 | 能播放的欧美同性videos | 精品九九视频 | 91网红福利精品区一区二 | 恩爱夫妇交换小说 |