Python爬虫+tkinter界面实现历史天气查询的思路详解


Posted in Python onFebruary 22, 2021

今天给大家分享用Python 爬虫+tkinter界面来实现历史天气查询。

一、实现效果

运行效果

运行效果如下:

Python爬虫+tkinter界面实现历史天气查询的思路详解 

二、基本思路

导入用到的库

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/

该网站提供了全国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', '  查询结果如下  \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()
# 设置窗口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界面实现历史天气查询的思路详解 

结语

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对小编的支持。

获得 本文完整代码

链接: https://pan.baidu.com/s/1ZCSRIqoOjrxTcLxLYOFiiQ 提取码: wb4f

到此这篇关于Python爬虫+tkinter界面实现历史天气查询的思路详解的文章就介绍到这了,更多相关Python爬虫tkinter界面历史天气查询内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python 运算符 供重载参考
Jun 11 Python
Java及python正则表达式详解
Dec 27 Python
python 将列表中的字符串连接成一个长路径的方法
Oct 23 Python
对Python3 序列解包详解
Feb 16 Python
Python matplotlib绘制饼状图功能示例
Sep 10 Python
如何关掉pycharm中的python console(图解)
Oct 31 Python
Python Lambda函数使用总结详解
Dec 11 Python
使用Pyhton 分析酒店针孔摄像头
Mar 04 Python
python json.dumps中文乱码问题解决
Apr 01 Python
python对接ihuyi实现短信验证码发送
May 10 Python
浅谈pytorch中的dropout的概率p
May 27 Python
Python实现抖音热搜定时爬取功能
Mar 16 Python
Python爬虫设置Cookie解决网站拦截并爬取蚂蚁短租的问题
Feb 22 #Python
Python爬虫爬取微博热搜保存为 Markdown 文件的源码
Feb 22 #Python
Python爬虫制作翻译程序的示例代码
Feb 22 #Python
Python爬虫爬取ts碎片视频+验证码登录功能
Feb 22 #Python
sklearn中的交叉验证的实现(Cross-Validation)
Feb 22 #Python
Python爬虫分析微博热搜关键词的实现代码
Feb 22 #Python
anaconda升级sklearn版本的实现方法
Feb 22 #Python
You might like
PHP - Html Transfer Code
2006/10/09 PHP
PHP数字和字符串ID互转函数(类似优酷ID)
2014/06/30 PHP
php使用mysqli向数据库添加数据的方法
2015/03/20 PHP
php+ajax实现无刷新的新闻留言系统
2020/12/21 PHP
Json_encode防止汉字转义成unicode的方法
2016/02/25 PHP
PHP版本的选择5.2.17 5.3.27 5.3.28 5.4 5.5兼容性问题分析
2016/04/04 PHP
Mac版PhpStorm之XAMPP整合apache服务器配置的图文教程详解
2016/10/13 PHP
JSQL 基于客户端的成绩统计实现方法
2010/05/05 Javascript
js获取单选框或复选框值及操作
2012/12/18 Javascript
js实现选中复选框文字变色的方法
2015/08/14 Javascript
详解Javascript继承的实现
2016/03/25 Javascript
移动端横屏的JS代码(beta)
2016/05/16 Javascript
详解如何解决Vue和vue-template-compiler版本之间的问题
2018/09/17 Javascript
150行Node.js实现的dns代理工具
2019/08/02 Javascript
layui添加动态菜单与选项卡 AJAX请求的例子
2019/09/25 Javascript
vue的路由映射问题及解决方案
2019/10/14 Javascript
Python中函数的用法实例教程
2014/09/08 Python
Python自动化部署工具Fabric的简单上手指南
2016/04/19 Python
Python+Opencv识别两张相似图片
2020/03/23 Python
python实现nao机器人身体躯干和腿部动作操作
2019/04/29 Python
python实现在函数图像上添加文字和标注的方法
2019/07/08 Python
Python 类属性与实例属性,类对象与实例对象用法分析
2019/09/20 Python
Python 中如何实现参数化测试的方法示例
2019/12/10 Python
python3.6.8 + pycharm + PyQt5 环境搭建的图文教程
2020/06/11 Python
matplotlib基础绘图命令之imshow的使用
2020/08/13 Python
HTML5进度条特效
2014/12/18 HTML / CSS
美国孕妇装品牌:Destination Maternity
2018/02/04 全球购物
Claire’s法国:时尚配饰、美容、珠宝、头发
2021/01/16 全球购物
小学生自我评价范例
2013/09/24 职场文书
销售心得体会
2014/01/02 职场文书
2014年党支部学习材料
2014/05/19 职场文书
霸气队列口号
2014/06/18 职场文书
优秀应届本科生求职信
2014/07/19 职场文书
小学英语教师研修感悟
2015/11/18 职场文书
2016年全国爱眼日宣传教育活动总结
2016/04/05 职场文书
详解MongoDB的条件查询和排序
2021/06/23 MongoDB