Python 爬虫+tkinter界面 实现历史天气查询
文章目录
- 一、实现效果
- 1. python代码
- 2. 运行效果
- 二、基本思路
- 1. 爬虫部分
- 2. tkinter界面
很多人学习python,不知道从何学起。
很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。
很多已经做案例的人,却不知道如何去学习更加高深的知识。
那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代码!
QQ群:101677771
一、实现效果
1. python代码
import requests
from lxml import etree
import re
import tkinter as tk
from PIL import Image, ImageTk
from 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-1date = 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月 前面加 0tm_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)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. 运行效果
运行效果如下:
二、基本思路
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个地区的历史天气预报查询,数据来源于城市当天的天气信息,可以查询到历史天气气温,历史风向,历史风力等历史天气状况。
分析网页可以发现,某个地区、某个月的所有天气数据的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-1date = 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月 前面加 0tm_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界面 实现历史天气查询相关推荐
- Python 爬虫+tkinter界面 实现历史天气查询
越长大我才越明白,前途,真的比什么都重要. 文章目录 一.实现效果 1. python代码 2. 运行效果 二.基本思路 1. 爬虫部分 2. tkinter界面 原文链接:https://yetin ...
- 历史天气查询 Android 版本 JAVA
古老版本:历史温度记录 精细版本:历史天气查询 现在已经可以在豌豆荚应用上搜索到,有用到的朋友可以试试看,提提建议,能下的就装一下然后再卸了,怎么滴一个软件下载量不能为0吧. 功能简单: 1,输入地区 ...
- 全国历史天气查询/历史天气预报查询——全国各月份数据爬取
全国历史天气查询/历史天气预报查询--全国各月份数据爬取 效果 图1 目标爬取数据 图2 最终实验效果 实验效果:最终可将官网已有的数据进行爬取整理,共363个城市,从2011年1月--至今 数据已上 ...
- python通过tkinter界面库实现三角形成立的测试
python通过tkinter界面库实现三角形成立的测试 from tkinter import * from tkinter import messagebox login = Tk() login ...
- 【python】tkinter界面化+百度API—聊天机器人(四)
目录 百度API tkinter界面设计 完整代码 实现结果如下: 百度API 这里聊天机器人的功能也是结合第一篇的语音识别([python]tkinter界面化+百度API-语音识别_张顺财的博客- ...
- 免费天气预报查询 API、历史天气查询 API 接口使用示例【源码可用】
天气预报查询 API.历史天气查询 API 接口使用示例[源码可用] 福利彩蛋:没有好玩的 API 接口?上百款免费接口等你来,免费 API,免费 API 大全 一.免费天气预报查询 API 通过城市 ...
- html查询历史天气,历史天气查询2345,怎样查以前的预报天气
历史天气查询2345,怎样查以前的预报天气以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 怎样查以前的预报天气 1.查历 ...
- 关于在python的tkinter界面中镶嵌mayplotlib动态图
关于在python的tkinter界面中镶嵌mayplotlib动态图 很多的时候,我们需要给客户展示一些比较美观的界面,中间就必然需要一些精美的图表,让客户看起来更加的专业,因此,我们就需要tkin ...
- python爬取历史天气查询_Python爬虫实战-爬取历史天气数据
最近项目需要对合肥市历史天气数据进行爬取,分析了几个天气数据网站,最终选择天气后报网站. 将实现过程遇到的问题以及下来,供以后参考. 步骤分析 这里我使用的是Python中的requests库Beau ...
最新文章
- 打一针就可修复受损心脏,“癌症克星”CAR-T跨界疗法登上Science封面
- 数据是互联网下半场产品人突围之道
- 一级建造师考试通过了 !
- mysql da_DA面板如何管理Mysql数据库?
- ubuntu16.04环境下使用ros运行ORB-SLAM3
- Powershell About Active Directory Server
- pathlib2 Path glob rglob的最新研究成果
- 慕课网仿去哪儿项目笔记--(四)-城市页面的优化
- net导出到excel数字变为科学技术法
- 计算机视觉教程2-5:图像金字塔送她一朵玫瑰(附代码)
- 计算机一级两列怎么筛选,怎么筛选Excel软件两个表格里相同的数据
- vue+elementUI中Dialog实现组件弹框以及子父组件页面传值
- hive sql系列(二)——统计每个人每个月访问量和累计月访问量
- 程序 多核优化 linux,linux 多核CPU性能调优
- Conda环境无法激活
- 仿真器VCS solver 对包含 function 约束的求解
- 学习区块链要掌握哪些专项能力?区块链学习培训多长时间?
- sql注入学习笔记(4)--sqlmap注入心得
- 用blender和MakeHuman生成人体动画
- 带倍速播放的播放器_带有HTML5的MP3播放器