最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年08月23日 09:25:51   作者:葉庭云  
這篇文章主要介紹了Python tkinter界面實(shí)現(xiàn)歷史天氣查詢的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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

1. python代碼

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)槠匆?
  # 處理用戶輸入的時(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)頁規(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 = ['日期:', '最高氣溫:', '最低氣溫:', '天氣:', '風(fēng)向:']
  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="時(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)行效果如下:

二、基本思路

導(dǎo)入用到的庫

import requests
from lxml import etree
import re
import tkinter as tk
from PIL import Image, ImageTk
from xpinyin import Pinyin

1. 爬蟲部分

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

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

分析網(wǎng)頁可以發(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ī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)頁發(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 = ['日期:', '最高氣溫:', '最低氣溫:', '天氣:', '風(fēng)向:']
  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界面

代碼如下:

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="時(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界面效果如下:

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

相關(guān)文章

  • python實(shí)現(xiàn)暗通道去霧算法的示例

    python實(shí)現(xiàn)暗通道去霧算法的示例

    這篇文章主要介紹了python實(shí)現(xiàn)暗通道去霧算法的示例,幫助大家更好的利用python處理圖像,感興趣的朋友可以了解下
    2020-09-09
  • python命令行工具Click快速掌握

    python命令行工具Click快速掌握

    這篇文章主要介紹了python命令行工具Click快速掌握,寫 Python 的經(jīng)常要寫一些命令行工具,雖然標(biāo)準(zhǔn)庫提供有命令行解析工具 Argparse,但是寫起來非常麻煩,我很少會(huì)使用它。命令行工具中用起來最爽的就是 Click,,需要的朋友可以參考下
    2019-07-07
  • Pygame游戲開發(fā)之太空射擊實(shí)戰(zhàn)盾牌篇

    Pygame游戲開發(fā)之太空射擊實(shí)戰(zhàn)盾牌篇

    相信大多數(shù)8090后都玩過太空射擊游戲,在過去游戲不多的年代太空射擊自然屬于經(jīng)典好玩的一款了,今天我們來自己動(dòng)手實(shí)現(xiàn)它,在編寫學(xué)習(xí)中回顧過往展望未來,在本課中,我們將為玩家添加一個(gè)盾牌以及一個(gè)用于顯示盾牌等級(jí)的欄
    2022-08-08
  • 快速解決Django關(guān)閉Debug模式無法加載media圖片與static靜態(tài)文件

    快速解決Django關(guān)閉Debug模式無法加載media圖片與static靜態(tài)文件

    這篇文章主要介紹了快速解決Django關(guān)閉Debug模式無法加載media圖片與static靜態(tài)文件的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Python實(shí)現(xiàn)八大排序算法

    Python實(shí)現(xiàn)八大排序算法

    這篇文章主要介紹了Python實(shí)現(xiàn)八大排序算法,如何用Python實(shí)現(xiàn)八大排序算法,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 使用Python實(shí)現(xiàn)炫酷的數(shù)據(jù)動(dòng)態(tài)圖大全

    使用Python實(shí)現(xiàn)炫酷的數(shù)據(jù)動(dòng)態(tài)圖大全

    數(shù)據(jù)可視化是通過圖形、圖表、地圖等可視元素將數(shù)據(jù)呈現(xiàn)出來,以便更容易理解、分析和解釋,它是將抽象的數(shù)據(jù)轉(zhuǎn)化為直觀形象的過程,本文給大家介紹了使用Python實(shí)現(xiàn)炫酷的數(shù)據(jù)動(dòng)態(tài)圖大全,需要的朋友可以參考下
    2024-06-06
  • Python OpenCV讀取視頻報(bào)錯(cuò)的問題解決

    Python OpenCV讀取視頻報(bào)錯(cuò)的問題解決

    大家好,本篇文章主要講的是Python OpenCV讀取視頻報(bào)錯(cuò)的問題解決,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • Python鍵盤輸入轉(zhuǎn)換為列表的實(shí)例

    Python鍵盤輸入轉(zhuǎn)換為列表的實(shí)例

    今天小編就為大家分享一篇Python鍵盤輸入轉(zhuǎn)換為列表的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • python 獲取微信好友列表的方法(微信web)

    python 獲取微信好友列表的方法(微信web)

    今天小編就為大家分享一篇python 獲取微信好友列表的方法(微信web),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • Python一行代碼對(duì)話ChatGPT實(shí)現(xiàn)詳解

    Python一行代碼對(duì)話ChatGPT實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了Python一行代碼對(duì)話ChatGPT實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03

最新評(píng)論

武穴市| 南雄市| 报价| 海丰县| 射洪县| 祁连县| 汉沽区| 大化| 安西县| 沅陵县| 苍梧县| 湘乡市| 香格里拉县| 吉隆县| 易门县| 武功县| 察雅县| 西和县| 周至县| 黄石市| 鱼台县| 大城县| 永清县| 县级市| 辽源市| 普兰县| 辉县市| 永城市| 顺平县| 五莲县| 达尔| 响水县| 石首市| 名山县| 新源县| 鹰潭市| 申扎县| 大兴区| 朝阳县| 赣州市| 焦作市|