文章目录

一、实现效果

很多人学习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 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('-')) # 获取地区文本框的输入 变为拼音
# 处理用户输入的时间
# 规定三种格式都可以 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) 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-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界面 实现历史天气查询的更多相关文章

  1. python爬虫之12306网站--火车票信息查询

    python爬虫之12306网站--火车票信息查询 思路: 1.火车票信息查询是基于车站信息查询,先完成车站信息查询,然后根据车站信息查询生成的url地址去查询当前已知出发站和目的站的所有车次车票信息 ...

  2. python爬虫之12306网站--车站信息查询

    python爬虫查询车站信息 目录: 1.找到要查询的url 2.对信息进行分析 3.对信息进行处理 python爬虫查询全拼相同的车站 目录: 1.找到要查询的url 2.对信息进行分析 3.对信息 ...

  3. 【Python爬虫】第四课(查询照片拍摄地址)

    首先,要能够查询到照片地址,查询的照片必须要开GPS拍,且上传时用原图…… 查询图片的exif信息,使用exifread包 import exifread img = exifread.process ...

  4. Python 爬虫实现天气查询(可视化界面版)

    github项目地址:StarMan Python 实现天气查询的程序早已完成,近日开学无课,昨晚心血来潮想做一个较为友好的界面版本,便匆忙行动了起来. 在之前已有的程序的基础上使用Tkinter 模 ...

  5. 用Python和Pandas以及爬虫技术统计历史天气

    背景 最近在计划明年从北京rebase到深圳去,所以最近在看深圳的各个方面.去年在深圳呆过一段时间,印象最深的是,深圳总是突然就下雨,还下好大的雨.对于我这种从小在南方长大但是后面又在北京呆了2年多的 ...

  6. 【python爬虫】用python编写LOL战绩查询

    介绍一个简单的python爬虫,通过Tkinter创建一个客户端,当输入要查询的LOL用户名称的时候,可以显示出当前用户的所在服务器,当前战力和当前段位. 爬取网页地址:http://lol.duow ...

  7. 爱奇艺用券付费VIP电影+python爬虫程序+可视化界面+下载本地

    申明:本博客中的工具及源码仅供个人学习使用,请勿用作商业等其他任何违法用途!否则后果自负 直接步入正题吧! 工具开发环境:windows10,python3.6 工具界面设计:基于python 自带的 ...

  8. Python爬虫入门教程 53-100 Python3爬虫获取三亚天气做旅游参照

    爬取背景 这套课程虽然叫爬虫入门类课程,但是里面涉及到的点是非常多,十分检验你的基础掌握的牢固程度,代码中的很多地方都是可以细细品味的. 为什么要写这么一个小东东呢,因为我生活在大河北,那雾霾醇厚的很 ...

  9. python爬虫之天气预报网站--查看最近(15天)的天气信息(正则表达式)

    python爬虫之天气预报网站--查看最近(15天)的天气信息(正则表达式) 思路: 1.首先找到一个自己想要查看天气预报的网站,选择自己想查看的地方,查看天气(例:http://www.tianqi ...

随机推荐

  1. 玩转 SpringBoot2.x 之整合邮件发送

    序 在实际项目中,经常需要用到邮件通知功能.比如,用户通过邮件注册,通过邮件找回密码等:又比如通过邮件发送系统情况,通过邮件发送报表信息等等,实际应用场景很多. 原文地址:https://www.mm ...

  2. .NetCore 入门

    .net core是什么? .net core是一个可以用来构建现代.可伸缩和高性能的跨平台软件应用程序的通用开发框架. 我们为什么要使用.net core,也就是说.net core有什么好处? 跨 ...

  3. 用python包xlwt将数据写入Excel中

    一般用两种格式的数据写入,不多说放上demo. 1.列表形式写入 import xlwt def data_write(file_path, datas): f = xlwt.Workbook() s ...

  4. ORACLE常用语句:

    ORACLE常用语句: 1.首先,创建(新)用户: create user username identified by password; username:新用户名的用户名 password: 新 ...

  5. 修改jar包配置文件的正确操作,jar包解压出来的文件夹重新打成jar,不依靠开发工具!!!!

    修改jar包配置文件的正确操作,有的时候通过一些解压工具可以对内部的文件进行修改,但是有时候会无效.这就很烦了 一.背景:       有一个springboot项目,事先我已经用编译好打成jar包以 ...

  6. Good-turning估计

    在学习NLP过程中,遇到了Good-turning的介绍,网上找了相关的资料查看,总结如下. 思想: 其主要思想是从概率的总量中分配少量的比例给零概率项. 思路: 假定给定的语料库中出现 \(r\)次 ...

  7. ROS 八叉树地图构建 - 安装 octomap 和 octomap_server 建图包!

    项目要用到八叉树库 Octomap 来构建地图,这里记录下安装.可视化,并启用带颜色的 Octomap 的过程. 一.Apt 安装 Octomap 库 如果你不需要修改源码,可以直接安装编译好的 oc ...

  8. python中1 is True 的结果为False,is判断与==判断的区别

    python中1 is True 的结果为False,而1 == True的结果为True. python中True的数值就是1,那为什么1 is True 的结果为False呢? 因为is判断和== ...

  9. 在阿里云托管kubernetes上利用 cert-manager 自动签发 TLS 证书[无坑版]

    前言 排错的过程是痛苦的也是有趣的. 运维乃至IT,排错能力是拉开人与人之间的重要差距. 本篇会记录我的排错之旅. 由来 现如今我司所有业务都运行在阿里云托管kubernetes环境上,因为前端需要对 ...

  10. 树莓派 4B VNC Viewer 显示 cannot currently show the desktop 的解决方法 (图文)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/ZChen1996/article/de ...