爬虫API腾讯疫情接口QT显示
自己一个做的一个小东西,很多部分不太完善,不喜勿喷!
如果大神有小建议和指点的话,欢迎欢迎欢迎!!!
原理介绍
- 通过request请求,获得数据用Json解析数据,对数据提取,将这个写成一个py文件留出用于QT文件的接口。
- 用pyqt_tool制作qt界面并生成py文件。
- 将两个结合到一块,最后用pyinstaller打包生成exe文件
代码展示
1. accquiredata.py模块
用于抓取API接口,并对于数据处理的包
class accquire(object):
d = {}
f = {}
date_list = [] # 日期
confirm_list = [] # 确诊
suspect_list = [] # 疑似
dead_list = [] # 死亡
heal_list = [] # 治愈
def __init__(self):
self.catch_distribution()
# self.catch_daily()
def catch_distribution(self):
"""抓取行政区域确诊分布数据"""
url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%d'%int(time.time()*1000)
data = json.loads(requests.get(url=url).json()['data'])
# print(data.keys())
self.d = data['areaTree'][0]['children']
self.f = [item['name'] for item in self.d]
def catch_daily(self):
"""抓取每日确诊和死亡数据"""
url = 'https://view.inews.qq.com/g2/getOnsInfo?name=wuwei_ww_cn_day_counts&callback=&_=%d'%int(time.time()*1000)
data = json.loads(requests.get(url=url).json()['data'])
data.sort(key=lambda x:x['date'])
for item in data:
month, day = item['date'].split('/')
self.date_list.append(datetime.strptime('2020-%s-%s'%(month, day), '%Y-%m-%d'))
self.confirm_list.append(int(item['confirm']))
self.suspect_list.append(int(item['suspect']))
self.dead_list.append(int(item['dead']))
self.heal_list.append(int(item['heal']))
def Find_Data(self,name):
na = name.split(',')
c = self.f.index(na[0])
data = {}
data[na[0]] = self.d[c]['total']['confirm']
for item in self.d[c]['children']:
if item['name'] not in data:
data[item['name']]={'today':item['today'],'total':item['total']}
# data.update({item['name']:item['total']['confirm']})
return data[na[0]],data[na[1]]['total']['confirm'],data[na[1]]['total']['suspect']
def plot_daily(self):
"""绘制每日确诊和死亡数据"""
self.catch_daily() # 获取数据
2. PyQT_Form.py模块
用于创建qt界面
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1083, 784)
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(85, 0, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(85, 0, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(85, 0, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
Form.setPalette(palette)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(230, 100, 91, 31))
self.pushButton.setObjectName("pushButton")
self.city = QtWidgets.QLineEdit(Form)
self.city.setEnabled(True)
self.city.setGeometry(QtCore.QRect(160, 150, 211, 31))
self.city.setText("")
self.city.setClearButtonEnabled(False)
self.city.setObjectName("city")
self.lineEdit_2 = QtWidgets.QLineEdit(Form)
self.lineEdit_2.setGeometry(QtCore.QRect(510, 150, 331, 51))
self.lineEdit_2.setPlaceholderText("")
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_1 = QtWidgets.QLineEdit(Form)
self.lineEdit_1.setGeometry(QtCore.QRect(510, 70, 331, 51))
self.lineEdit_1.setPlaceholderText("")
self.lineEdit_1.setObjectName("lineEdit_1")
self.lineEdit_4 = QtWidgets.QLineEdit(Form)
self.lineEdit_4.setGeometry(QtCore.QRect(510, 230, 331, 51))
self.lineEdit_4.setPlaceholderText("")
self.lineEdit_4.setObjectName("lineEdit_4")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(200, 300, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayoutWidget = QtWidgets.QWidget(Form)
self.gridLayoutWidget.setGeometry(QtCore.QRect(150, 340, 701, 351))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.retranslateUi(Form)
self.pushButton.clicked.connect(Form.pushButton_click)
self.pushButton_2.clicked.connect(Form.plot_click)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "确定"))
self.city.setPlaceholderText(_translate("Form", "请输入城市(如 陕西,西安)"))
self.lineEdit_2.setText(_translate("Form", "省总数目: "))
self.lineEdit_1.setText(_translate("Form", "确诊数目: "))
self.lineEdit_4.setText(_translate("Form", "疑似病例: "))
self.pushButton_2.setText(_translate("Form", "走势"))
3. main.py
最后是主程序啦,用于把上述两个程序结合到一块就是主程序啦
class MyPyQT_Form(QtWidgets.QWidget,Ui_Form,accquire):
def __init__(self):
super(MyPyQT_Form,self).__init__()
self.setupUi(self)
# pg.setConfigOption('background', 'w')
self.pw = pg.PlotWidget(title = "病毒人数曲线")
self.pw.enableMouse()
self.pw.setLabel(axis = "left", text = "/people")
self.pw.setLabel(axis = "bottom",text = "time")
self.pw.addLegend()
# self.pw.setLable(axis="left", text="/people")
self.gridLayout.addWidget(self.pw)
# self.catch_daily()
self.catch_distribution()
def pushButton_click(self):
self.a = self.city.text()
b,q,f= self.Find_Data(self.a)
q = "确诊数目: "+str(q)
b = "省总数目: "+str(b)
f = "疑似病例: "+str(f)
self.lineEdit_1.setText(q)
self.lineEdit_2.setText(b)
self.lineEdit_4.setText(f)
return
def plot_click(self):
self.plot()
return
def plot(self):
self.plot_daily()
self.pw.plot(self.confirm_list,pen=(255,0,0), name="identify")
self.pw.plot(self.suspect_list, pen=(0,255,0), name="suspect")
self.pw.plot(self.dead_list, pen=(0,25,255), name="dead")
self.pw.plot(self.heal_list, pen=(175,0,255), name="deal")
def paintEvent(self, event):# set background_img
painter = QPainter(self)
painter.drawRect(self.rect())
pixmap = QPixmap("./img/encourage.jpg")
painter.drawPixmap(self.rect(), pixmap)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
my_pyqt_form = MyPyQT_Form()
my_pyqt_form.paintEngine()
my_pyqt_form.show()
sys.exit(app.exec_())
界面展示
功能说明
1.可以查询全国的地区的情况
tip:输入“,”的时候需要以英文的形式,如:陕西,西安
2.可以看清楚从病毒传染开始到现在的走势