Python——爬取中国气象台网站的天气信息并用Qt显示

阅读: 评论:0

Python——爬取中国气象台网站的天气信息并用Qt显示

Python——爬取中国气象台网站的天气信息并用Qt显示

几个月前写的,写的有点垃圾,大神勿喷。

爬取网站:/

我写了两个.py文件,分别是getjson.py和weather.py。

getjson.py是得到目标网站每个省对应的每个市和每个市的url,大体格式为{“province1”:{“city1”:url,“city2”:url,},“province2”:{“city1”:url,“city2”:url,},},将得到的字典转化为json并写入文件中,这样就不需要一直访问目标网站了。

weather.py就是得到目标城市天气信息并用PyQt5显示出来。

getjson.py:

quest
import json
from lxml import etreeclass get_weather():def __init__(self):self.province_url = '/'self.base_url1 = '/'self.pro = {}self.province_code = {}_provice(self.province_url)def open_url(self,url):req = quest.Request(url)req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.89 Safari/537.36')response = quest.urlopen(req)html = responsereturn htmldef get_city(self,url):html = self.open_url(url).read().decode('utf-8')target = json.loads(html)ls = {}for i in target:ls[i['city']] = '/' + i['code']return lsdef get_provice(self,url):html = self.open_url(url).read().decode('utf-8')target = json.loads(html)for each in target:self.province_code[each['name']] = '/'+ each['code'] #省份:codefor each in target:#每一个省对应所有城市名和城市名代号self.pro[each['name']] = _city(self.province_code[each['name']]) #得到每个省对应的每个市print(self.pro)def main():w = get_weather()print("writing")with open('weather.json', 'w') as f:json.dump(w.pro,f)print("finish")
if __name__ == '__main__':main()

weather.py

from PyQt5.QtWidgets import QWidget, QMainWindow, QAction, QVBoxLayout, QLineEdit, QPushButton, QTextEdit, QMessageBox, QApplication
from PyQt5.QtGui import QTextCursor, QIcon
from PyQt5.QtNetwork import QTcpSocket
from PyQt5.QtCore import Qt
from PyQt5 import QtWidgets,QtGui
import sys
import json
questdef open_url(url):req = quest.Request(url)req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.89 Safari/537.36')response = quest.urlopen(req)return responsedef get_weatherinfo(prov, city):html = open_url(pro[prov][city]).read().decode('utf-8')target = json.loads(html)weather_info = {}weather_info['城市'] = cityweather_info['发布时间'] = target['publish_time']weather_info['当前温度'] = str(target['weather']['temperature']) + '℃'weather_info['体感温度'] = str(target['weather']['feelst']) + '℃'weather_info['天气情况'] = str(target['weather']['info'])weather_info['相对湿度'] = str(target['weather']['humidity']) + '%'weather_info['降水量'] = str(target['weather']['rain']) + 'mm'weather_info['风向'] = str(target['wind']['direct']) + ',' + str(target['wind']['power']) + ',风速' + str(target['wind']['speed']) + 'm/s'return weather_infoclass Window(QWidget):def __init__(self):super().__init__()self.initUI()self.pro = ""self.city = ""def initUI(self):self.setWindowTitle('天气查询系统')self.setWindowIcon(QIcon('QQ.ico'))self.setGeometry(600,400,400,400)self.lay = QVBoxLayout(un1 = QtWidgets.QComboBox(un2 = QtWidgets.QComboBox(self)self.item = QtWidgets.QTextEdit(self)self.item.setReadOnly(True)self.item.setStyleSheet('color:green')font = QtGui.QFont()font.setFamily('微软雅黑')font.setWeight(100)font.setPointSize(10)self.item.setFont(font)ve(0,80)size(400,size(400,ve(0,size(400,40)self.lay.un1)self.lay.un2)self.lay.addWidget(self.item)for each in un1.addItem(each)for each in un1.currentText()]:un2.addItem(_info()def get_info(self):self.item.clear()pro = un1.currentText()city = un2.currentText()try:info = get_weatherinfo(pro, city)except:QMessageBox.information(self,'错误', '查询天气失败')returnfor each in info:self.item.insertPlainText(each + ':' + info[each] + 'n')veCursor(QTextCursor.End)def nextmeau(self):un2.clear()for each in un1.currentText()]:un2.addItem(_info()def onAction(self):_info()if __name__ == '__main__':with open('weather.json', 'r') as f:pro = json.load(f)app = QApplication(sys.argv)w = Window()w.show()_())

QQ.ico:(图标)

先运行getjson.py得到各个城市的url,然后在运行weather.py。

下面是运行效果图:


本文发布于:2024-01-29 02:00:30,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170646483311914.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:气象台   中国   天气   网站   信息
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23