pyqt5+爬虫实现简单的天气的展示

前言

调用的api是和风天气的api。

正文

主页面

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from display import Display
from init import weather_data
# weather_data 爬虫返回的数据
class only(QWidget):
    def __init__(self):
        super().__init__()
        self.get = None
        self.label = None
        self.cin = None
        self.ok = None
        self.lose = None
        self.data=None
#可以不要,不影响
        self.initui()

    def initui(self):
        self.resize(565, 364)
# 按钮
        self.ok = QPushButton(self)
        self.ok.setText("确定")
        self.ok.setGeometry(140, 240, 93, 28)
        self.ok.clicked.connect(self.a)
#输入框
        self.cin = QLineEdit(self)
        self.cin.setGeometry(140, 170, 211, 21)
#标签
        self.label = QLabel(self)
        self.label.setText("输入城市")
        self.label.setGeometry(210, 120, 72, 15)
#按钮
        self.lose = QPushButton(self)
        self.lose.setGeometry(270, 240, 93, 28)
        self.lose.setText("取消")
        self.lose.clicked.connect(self.b)

        self.setWindowTitle('天气预报')
#确定事件
    def a(self):
        if not self.ok.isChecked():
            text=self.cin.text()
            self.get=weather_data()
            self.data=self.get.start(text)
            display=Display(self.data)
#取消事件
     def b(self):
            self.close()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle(QStyleFactory.create('Fusion'))
    a=only()
    a.show()
    sys.exit(app.exec_())
# 先用qt designer 设置出页面
# 简单点,就是一个lanbel,一个lineedit,两个按钮
# 再用pyuic把ui文件转换成py(或者python命令)
# 再对转换后的py文件进行修改

如图

pyqt5+爬虫实现简单的天气的展示_第1张图片

输入一个城市

比如--成都

展示如下页面

pyqt5+爬虫实现简单的天气的展示_第2张图片

就一个QTableWidget和一个QPushButton控件。

整个页面是个对话框

display.py页面

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


class Display(QDialog):
    def __init__(self,a:list):
#需要闯入参数a
        super().__init__()
        self.data=a
        self.initui()

    def initui(self):
        self.resize(1704, 616)
        self.setWindowFlags(Qt.WindowMinMaxButtonsHint | Qt.WindowCloseButtonHint)
        self.setWindowTitle("展示框")
        self.init_insert_data()
        close = QPushButton(self)
        close.setGeometry(1600, 580, 93, 28)
        close.setText("关闭")
        close.clicked.connect(self.a)
        self.show()
        self.exec_()
#插入数据
    def init_insert_data(self):
        header = self.data[0]
        table = QTableWidget(0, len(header),self)
        table.setGeometry(10, 31, 1640, 300)
        for i, t in enumerate(header):
            item = QTableWidgetItem()
            item.setText(f'{t}')
            table.setHorizontalHeaderItem(i, item)
        del self.data[0]

        row_count = table.rowCount()
        for i in self.data:
            table.insertRow(row_count)
            for ids, t in enumerate(i):
                cell = QTableWidgetItem(str(t))
                table.setItem(row_count, ids, cell)
            row_count += 1
    def a(self):
        self.close()

爬虫页面

import requests
import pandas as pd
import prettytable as pt
tb = pt.PrettyTable()
class weather:
    def __init__(self):
        pass
    def start(self,a):
        s = pd.read_csv(path)
#需要一个Location_ID,参数
        t = s.loc[f'{a}']['Location_ID']
        return self.get_data(t)
#链式调用
    def get_data(self,a,show=False):
        title = ['序号', '时间', '最高温', '最低温', '白天天气', '晚上天气', '白天的风', '白风级', '晚上的风', '晚风度', '湿度', '降水量', '紫外线']
        tb.field_names = ['序号','时间', '最高温', '最低温', '白天天气', '晚上天气', '白天的风', '白风级', '晚上的风', '晚风度', '湿度', '降水量', '紫外线']
        url = f'https://devapi.qweather.com/v7/weather/7d?key=需要申请&location={a}'
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.35'
        }
        r = requests.get(url=url, headers=headers)
        json_data = r.json()
        tians = json_data['daily']
        c = [title]
        num = 0
        for tian in tians:
            time = tian['fxDate']
            tempMax = tian['tempMax']
            tempMin = tian['tempMin']
            textDay = tian['textDay']
            textNight = tian['textNight']
            dayfeng = tian['windDirDay']
            dfengscale = tian['windSpeedDay']
            nightfeng = tian['windDirNight']
            nfengscale = tian['windSpeedNight']
            shidu = tian['humidity']
            water = tian['precip']
            zi_wai_xian = tian['uvIndex']
            tb.add_row([num,time,tempMax,tempMin,textDay,textNight,dayfeng,dfengscale,nightfeng,nfengscale,shidu,water,zi_wai_xian])
            c.append([num, time, tempMax, tempMin, textDay, textNight, dayfeng, dfengscale, nightfeng, nfengscale, shidu, water, zi_wai_xian])
            num += 1
        if show:
            print(tb)
            return c
        else:
            return c
#数据c为为列表

和风天气的控制台

你可能感兴趣的:(爬虫,python,开发语言)