文章目录

一、实现效果

很多人学习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. windows:shellcode 远程线程hook/注入(五)

    前面几篇文章介绍了通过APC注入.进程注入.windows窗口处理函数回调.kernercallback回调执行shellcode,今天继续介绍通过heap Spray(翻译成中文叫堆喷射)执行she ...

  2. django--各个文件的含义

    当你创建项目或者应用后你是不是发现多了很多个文件,现在我们来看看各代表什么意思 与你项目名相同的文件夹:是项目的管理功能目录,这个目录的名称因用户所创建的项目名称的不同而不同 在该目录下还有四个文件: ...

  3. 浅谈js数组中的length属性

    前言 一位正在学习前端的菜鸟,虽菜,但还未放弃. 内容 首先,我们都知道每个数组都有一个length属性 这个length属性一般我们用来循环遍历的约束,一般我们都会把他认为是该数组里面有几个元素这个 ...

  4. SqlServer 查询的几种方式以及数字函数、时间函数的应用总结(回归基础)

    --语法:select * from 表名 *表示查询所有字段数据 select * from Class select * from Student select * from RankingLis ...

  5. ios wkwebview didReceiveAuthenticationChallenge crash解决

    //需要响应身份验证时调用 同样在block中需要传入用户身份凭证 //现在就是不进行https验证了 然后就闪退不了了 - (void)webView:(WKWebView *)webView di ...

  6. 用MPI进行分布式内存编程(1)

    <并行程序设计导论>第三章部分程序 程序3.1运行实例 #include<stdio.h> #include<string.h> #include<mpi.h ...

  7. python6.2类的封装

    class Card(object): def __init__(self,num,pwd,ban): self.num=num#卡号 self.pwd=pwd#密码 self.__ban=ban#余 ...

  8. “随手记”开发记录day15

    今天完成了前两天没有完成的增加“修改”功能.对于已经添加的记账记录,长按可以进行修改和删除的操作.

  9. 【源码】Python3使用Requests抓取和检测电光代理API,并查询ip代理是否成功

    电光代理成立后,做一篇笔记,记录我使用Requests抓取和测试电光代理的方法 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做 ...

  10. CSS高级特效(上)

    1.CSS Shapes CSS Shapes是一个新标准,旨在让Web设计者能使用各种形状. CSS Shapes包含两组新属性,一组用于设置影响盒子中内容的形状,另一组用于设置影响形状元素周边内容 ...