实战Python:利用Python和PyQt5实现天气查询小系统

您所在的位置:网站首页 历史天气预报查询软件 实战Python:利用Python和PyQt5实现天气查询小系统

实战Python:利用Python和PyQt5实现天气查询小系统

2024-01-10 12:31| 来源: 网络整理| 查看: 265

北京今天温度降得厉害,我就在想,我已经很久没有关注天气了。 咦?天气?刚学了爬虫可以爬一下吗? 果然爬到了某个城市的天气。 前段时间刚学了Pyqt,可以做一个GUI吗? 果然···· 这个小的软件主要结合了两部分知识,第一部分就是爬虫的相关知识,第二部分就是构造一个GUI界面来展示我们获取到的数据。 一、获取到天气的数据

天气查询程序,首先要有天气情况。而我们,利用爬虫,抓包中华万年历得到的接口(JSON):

url = ‘http://wthrcdn.etouch.cn/weather_mini?city={}’.format(city_name)

我们所请求的网址是这个,请求后,会返回city_name的天气情况。

但需要注意的一点是,请求返回的结果是JSON 数据,我们需要调用json.loads()来将JSON数据转换成我们Python中的数据,也就是字典。更加便于我们后续的操作。 爬取天气部分的代码:

import requests import json headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36' } city_name = input() weather_url = 'http://wthrcdn.etouch.cn/weather_mini?city={}'.format(city_name) city_response = requests.get(weather_url, headers=headers) weather_dict = json.loads(city_response.text) print(type(weather_dict)) print(weather_dict)

我们输入北京运行后的输出结果为: 在这里插入图片描述 如果我们把完整的返回结果复制到文件中,可以看到,它其实是这样的: 在这里插入图片描述在上面两图中,我们可以得出信息:

我们用json.loads()解析请求后的结果的字符串后,返回值为字典类型

在返回值中,有保存三个键的数据。其中: “data”:数据类型为字典,其中保持着昨天到四天后共六天的天气情况 “desc”:请求结果,ok代表查询成功,invilad-citykey代表没有信息。 在"data"中: "yesterday"键值对:保存着昨天的天气情况数据 "city"键值对:保存查询城市名数据 "forecast"键值对:是一个列表,存储着今天到四天后共五天的天气情况数据 "ganmao"键值对:一个温馨提示 "wendu"键值对:实时温度

其中,我们需要的是请求结果data,以及请求是否成功desc。

二、构造GUI

我们已经有了天气数据,下一步就要构造GUI界面了。这个界面其实看个人如何构造,使用PyQt5。 我有两个界面,初始界面: 在这里插入图片描述 查询结果界面: 在这里插入图片描述 以及一个退出界面: 在这里插入图片描述 初始界面初始化相关代码为:

super().__init__() self.lb = QLabel(self) self.lb.setGeometry(70, 25, 80, 40) self.lb.setText('请输入城市:') self.textbox = QLineEdit(self) self.textbox.setGeometry(150, 30, 130, 30) self.findButton = QPushButton('查询', self) self.findButton.setGeometry(60, 85, 100, 40) self.quitButton = QPushButton('退出', self) self.quitButton.clicked.connect(self.close) self.findButton.clicked.connect(self.find_weather) self.quitButton.setGeometry(190, 85, 100, 40) self.setGeometry(500, 500, 350, 150) self.setWindowTitle('Icon') self.setWindowTitle('天气查询,目前仅支持单次查询') self.setWindowIcon(QIcon('weather.png')) self.show()

查询结果相关代码:

self.setGeometry(500, 500, 720, 300) title = city + "近五天天气状况" self.setWindowTitle(title) self.setWindowIcon(QIcon('weather_info.png')) lb = QLabel(self) lb.setGeometry(70, 25, 720, 50) lb.setText(info) lb.setStyleSheet("color:red;") lb_day1 = QLabel(self) lb_day1.setGeometry(20, 50, 120, 200) lb_day1.setText(day[0]) lb_day1 = QLabel(self) lb_day1.setGeometry(160, 50, 120, 200) lb_day1.setText(day[1]) lb_day1 = QLabel(self) lb_day1.setGeometry(300, 50, 120, 200) lb_day1.setText(day[2]) lb_day1 = QLabel(self) lb_day1.setGeometry(440, 50, 120, 200) lb_day1.setText(day[3]) lb_day1 = QLabel(self) lb_day1.setGeometry(580, 50, 120, 200) lb_day1.setText(day[4]) 二、添加事件,完成代码

首先贴上完整代码:

import sys import requests import json from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox, QPushButton, QLineEdit, QLabel from PyQt5.QtGui import QIcon # 定义请求头部的用户代理 # 简单的反爬虫措施 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36' } def reform_fl(str_fl): """ 将风力转换为正常输出 :param str_fl: 不正常输入 :return: 正常输出 """ new_str = str_fl.split("[")[2].split("]")[0] if new_str.startswith("


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3