I do not know how to use Python to create weather query software you will quickly come to see it out

Here Insert Picture Description
Previously, the public how to use a shared number PyQt5 make guessing game and timer, this time, we continue to learn: how to make use PyQt5 weather query software.

Development environment

Python3
PyQt5
requests

Ready to work

First get a weather codes corresponding to different cities,
from https://www.heweather.com/documents/city.html (end text get csv file) download the csv file
to get the csv file, we must first data preprocessing jobs.

import pandas as pd
# 将下载好的文件命名为 'city_code.csv'
file = pd.read_csv('city_code.csv')
# 选取需要的两列信息
file = file.loc[:,['City_ID', 'City_CN']]
# 读取前五行信息
file.head()

Here Insert Picture Description

# 匹配 City_ID 中的数字
def convert(x):
 pat = re.compile('(d+)')
 return pat.search(x).group()
file['City_ID_map'] = file['City_ID'].map(convert)
# 建立城市与代码之间的映射关系
def city2id(file):
 code_dict = {}
 key = 'City_CN'
 value = 'City_ID_map'
 for k, v in zip(file[key], file[value]):
 code_dict[k] = v
 return code_dict
code_dict = city2id(file)
# 将所得的字典数据存储为 txt 文件
import json
filename = 'city_code.txt'
with open(filename, 'w') as f:
 json.dump(code_dict, f)

After the dictionary is stored as txt file, then we only need to read the file, and then get the dictionary:

Python一群已满,二群学习基地开始招募906407826,学习资料面试技巧免费等你来拿

with open(filename, 'r') as f:
 text = json.load(f)

If you do not want to waste of effort to deal with these data can be used to provide the end the city_code.txt file directly.

Ui Design

Use Qt Designer, we can design the following interfaces:

Here Insert Picture Description

If you do not want to design the interface, you can import the file Ui_weather.py the end the offer directly.

Main logic:

This time we use the api interface: ' http://wthrcdn.etouch.cn/weather_mini?citykey={code} ', code that is processed before the city code, such as Changzhou city code: 101 191 101. Replace the variable code, sends a request, the site returned to us for some json file formats:

Here Insert Picture Description

According to json this statement, we can easily extract the required information:


# 天气情况
data = info_json['data']
city = f"城市:{data['city']}
"
today = data['forecast'][0]
date = f"日期:{today['date']}
"
now = f"实时温度:{data['wendu']}度
"
temperature = f"温度:{today['high']} {today['low']}
"
fengxiang = f"风向:{today['fengxiang']}
"
type = f"天气:{today['type']}
"
tips = f"贴士:{data['ganmao']}
"

Of course, we must first use requests.get method to get this json code.

def query_weather(code):
# 模板网页
 html = f'http://wthrcdn.etouch.cn
 /weather_mini?citykey={code}'
# 向网页发起请求
 try:
 info = requests.get(html)
 info.encoding = 'utf-8'
# 捕获 ConnectinError 异常
 except requests.ConnectionError:
 raise 
# 将获取的数据转换为 json 格式
 try:
 info_json = info.json()
# 转换失败提示无法查询
 except JSONDecodeError:
 return '无法查询'

Here we introduce a control method used in this article at:

# 将 textEdit 设置为只读模式
self.textEdit.setReadOnly(True)
# 将鼠标焦点放在 lineEdit 编辑栏里
self.lineEdit.setFocus()
# 获取 lineEdit 中的文本
city = self.lineEdit.text()
# 设置文本
self.textEdit.setText(info)
# 清空文本
self.lineEdit.clear()

Query button to set the shortcut keys:

def keyPressEvent(self, e):
# 设置快捷键
 if e.key() == Qt.Key_Return:
 self.queryWeather()

Finally, we can use the -w pyinstaller weather.py packaged applications, but remember to pack finished, the city_code.txt copied to dist / weather folder, otherwise the program can not run.

The above is the entire contents of the article.

Guess you like

Origin blog.csdn.net/PyhtonChen/article/details/94737631