Python making weather query software [python] will learn combat

 

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.
To obtain the source code and exe files, in a micro-channel public number Python高效编程backstage reply:  天气.
PS Note: Many people learn Python process will encounter a variety of problems to worry about, no one easy answer to give up. For this reason small series built a Python full-stack free Q & A skirt: Seven Yiyi nine hundred seventy-seven bar and the next five (digital homonym) conversion can be found, but not the older drivers have problems to solve, there is also the latest real Python Tutorial Free non-,, together under mutual supervision and common progress!

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',并删除 header
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:

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 I provided direct Ui_weather.py.

Main logic:

Api interfaces we use this as: ' 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, 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']}\n" today = data['forecast'][0] date = f"日期:{today['date']}\n" now = f"实时温度:{data['wendu']}度\n" temperature = f"温度:{today['high']} {today['low']}\n" fengxiang = f"风向:{today['fengxiang']}\n" type = f"天气:{today['type']}\n" tips = f"贴士:{data['ganmao']}\n" 

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() 

Set shortcut buttons for the query:

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

Finally, we can use Pyinstaller -w 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 this article, and more details, see the source code. To obtain the source code and exe files Note: Many people learn Python process will encounter a variety of problems to worry about, no one easy answer to give up. For this reason small series built a Python full-stack free Q & A skirt: Seven Yiyi nine hundred seventy-seven bar and the next five (digital homonym) conversion can be found, but not the older drivers have problems to solve, there is also the latest real Python Tutorial Free non-,, together under mutual supervision and common progress!

Text and images in this article from the network with their own ideas, only to learn, exchange, not for any commercial purposes, belongs to original author, if any questions, please contact us for treatment.

 
 

Guess you like

Origin www.cnblogs.com/chengxuyuanaa/p/12531206.html