PyQt5使用http请求获取天气

Posted acpie-liusiqi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PyQt5使用http请求获取天气相关的知识,希望对你有一定的参考价值。

weather.py

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtNetwork import *
import widget
import sys


class MyWeather(QWidget):
    def __init__(self, parent = None):
        super().__init__(parent)
        self.mywidget = widget.Ui_Widget()
        self.mywidget.setupUi(self)
        self.manager = QNetworkAccessManager(self)
        self.manager.finished.connect(self.replyFinished)
        self.mywidget.pushButton.clicked.connect(self.request)

    def replyFinished(self, reply):
        str1 = reply.readAll()
        str2 = bytes.decode(str1.data(),encoding=utf8)
        self.mywidget.textBrowser.setText(str2)
        err = QJsonParseError()
        json_recev = QJsonDocument.fromJson(str1,err)
        if not json_recev.isNull():
            myobject = json_recev.object()
            if data in myobject:
                myvalue = myobject[data]
                if myvalue.isObject():
                    myobject_data = myvalue.toObject()
                    if forecast in myobject_data:
                        value = myobject_data[forecast]
                        if value.isArray():
                            value1 = value.toArray()[0].toObject()
                            self.tianqi = value1[type].toString()
                            self.mywidget.lineEdit_2.setText(self.tianqi)
                            low = value1[low].toString()
                            high = value1[high].toString()
                            self.wendu = low[2:] + - + high[2:]
                            self.mywidget.lineEdit_3.setText(self.wendu)
                            fengxiang = value1[fengxiang].toString()
                            fengli = value1[fengli].toString()
                            self.fengli = fengxiang + fengli[9:-3]
                            self.mywidget.lineEdit_4.setText(self.fengli)



        reply.deleteLater()

    def request(self):
        city = self.mywidget.lineEdit.text() #str
        pre = http://wthrcdn.etouch.cn/weather_mini?city=
        res = pre + city
        print(res)
        url = QUrl(res)
        self.manager.get(QNetworkRequest(url))

if __name__ == __main__:
    app = QApplication(sys.argv)
    weather = MyWeather()
    weather.show()
    sys.exit(app.exec_())

widget.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file ‘widget.ui‘
#
# Created by: PyQt5 UI code generator 5.12.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Widget(object):
    def setupUi(self, Widget):
        Widget.setObjectName("Widget")
        Widget.resize(400, 300)
        self.pushButton = QtWidgets.QPushButton(Widget)
        self.pushButton.setGeometry(QtCore.QRect(300, 40, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.textBrowser = QtWidgets.QTextBrowser(Widget)
        self.textBrowser.setGeometry(QtCore.QRect(20, 10, 256, 161))
        self.textBrowser.setObjectName("textBrowser")
        self.formLayoutWidget = QtWidgets.QWidget(Widget)
        self.formLayoutWidget.setGeometry(QtCore.QRect(50, 179, 160, 111))
        self.formLayoutWidget.setObjectName("formLayoutWidget")
        self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
        self.formLayout.setContentsMargins(11, 11, 11, 11)
        self.formLayout.setSpacing(6)
        self.formLayout.setObjectName("formLayout")
        self.label = QtWidgets.QLabel(self.formLayoutWidget)
        self.label.setObjectName("label")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label)
        self.lineEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
        self.lineEdit.setObjectName("lineEdit")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.lineEdit)
        self.label_2 = QtWidgets.QLabel(self.formLayoutWidget)
        self.label_2.setObjectName("label_2")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2)
        self.lineEdit_2 = QtWidgets.QLineEdit(self.formLayoutWidget)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.lineEdit_2)
        self.label_3 = QtWidgets.QLabel(self.formLayoutWidget)
        self.label_3.setObjectName("label_3")
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_3)
        self.lineEdit_3 = QtWidgets.QLineEdit(self.formLayoutWidget)
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.lineEdit_3)
        self.label_4 = QtWidgets.QLabel(self.formLayoutWidget)
        self.label_4.setObjectName("label_4")
        self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.label_4)
        self.lineEdit_4 = QtWidgets.QLineEdit(self.formLayoutWidget)
        self.lineEdit_4.setObjectName("lineEdit_4")
        self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.lineEdit_4)

        self.retranslateUi(Widget)
        QtCore.QMetaObject.connectSlotsByName(Widget)

    def retranslateUi(self, Widget):
        _translate = QtCore.QCoreApplication.translate
        Widget.setWindowTitle(_translate("Widget", "Widget"))
        self.pushButton.setText(_translate("Widget", "获取天气"))
        self.label.setText(_translate("Widget", "城市"))
        self.label_2.setText(_translate("Widget", "天气"))
        self.label_3.setText(_translate("Widget", "温度"))
        self.label_4.setText(_translate("Widget", "风力"))

效果图:

技术图片

以上是关于PyQt5使用http请求获取天气的主要内容,如果未能解决你的问题,请参考以下文章

Python实战!四行Python代码就能知道你那的天气,你敢信吗?

python 天气爬虫

如何使用 openweathermap api 和 http 包获取每小时和 16 天的天气预报

在发送 http 请求时让 PyQt5 标签动态更新

通用jsonp跨域技术获取天气数据

学习 PyQt5。在我的代码片段中找不到错误 [关闭]