亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PyQt5+requests實現車票查詢工具

發布時間:2020-10-14 16:51:27 來源:腳本之家 閱讀:177 作者:__師寇__ 欄目:開發技術

PyQt5+requests實現一個車票查詢工具,供大家參考,具體內容如下

結構圖

PyQt5+requests實現車票查詢工具

效果圖

PyQt5+requests實現車票查詢工具

思路

1、search(QPushButton)點擊信號(clicked)連接到自定義的槽函數(event.search)
2、槽函數(event.search)接收四個參數:QTableWidget對象的引用、兩個QLabel中的內容(站臺名稱)、QDateEdit格式化日期
3、槽函數(event.search)調用爬蟲類(TrainService)獲取車次信息,添加到QTableWidget對象中。

代碼

1、ui.py(ui界面)

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

# Form implementation generated from reading ui file 'E:\Python\Qt\ui_0.ui'
#
# Created by: PyQt5 UI code generator 5.11.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
from Event import EventClass
class Ui_MainWindow(object):
 def setupUi(self, MainWindow):
 event=EventClass()
 MainWindow.setObjectName("MainWindow")
 MainWindow.resize(793, 604)
 MainWindow.setStyleSheet("font: 10pt \"Microsoft YaHei UI\";")
 self.centralwidget = QtWidgets.QWidget(MainWindow)
 self.centralwidget.setObjectName("centralwidget")
 self.widget = QtWidgets.QWidget(self.centralwidget)
 self.widget.setGeometry(QtCore.QRect(0, 0, 791, 43))
 self.widget.setObjectName("widget")
 self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
 self.horizontalLayout.setSizeConstraint(QtWidgets.QLayout.SetNoConstraint)
 self.horizontalLayout.setContentsMargins(10, 1, 1, 3)
 self.horizontalLayout.setSpacing(15)
 self.horizontalLayout.setObjectName("horizontalLayout")
 self.label = QtWidgets.QLabel(self.widget)
 self.label.setObjectName("label")
 self.horizontalLayout.addWidget(self.label)
 self.lineEdit = QtWidgets.QLineEdit(self.widget)
 self.lineEdit.setMaximumSize(QtCore.QSize(742, 16777215))
 self.lineEdit.setObjectName("lineEdit")
 self.horizontalLayout.addWidget(self.lineEdit)
 self.label_2 = QtWidgets.QLabel(self.widget)
 self.label_2.setObjectName("label_2")
 self.horizontalLayout.addWidget(self.label_2)
 self.lineEdit_2 = QtWidgets.QLineEdit(self.widget)
 self.lineEdit_2.setObjectName("lineEdit_2")
 self.horizontalLayout.addWidget(self.lineEdit_2)
 self.dateEdit = QtWidgets.QDateEdit(self.widget)
 self.dateEdit.setDate(QtCore.QDate(2019, 1, 1))
 self.dateEdit.setObjectName("dateEdit")
 self.horizontalLayout.addWidget(self.dateEdit)
 self.pushButton = QtWidgets.QPushButton(self.widget)
 self.pushButton.setObjectName("pushButton")
 self.horizontalLayout.addWidget(self.pushButton)
 self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
 self.scrollArea.setGeometry(QtCore.QRect(0, 40, 791, 561))
 self.scrollArea.setWidgetResizable(True)
 self.scrollArea.setObjectName("scrollArea")
 self.scrollAreaWidgetContents = QtWidgets.QWidget()
 self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 789, 559))
 self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
 self.tableWidget = QtWidgets.QTableWidget(self.scrollAreaWidgetContents)
 self.tableWidget.setGeometry(QtCore.QRect(0, 0, 791, 561))
 sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
 sizePolicy.setHorizontalStretch(0)
 sizePolicy.setVerticalStretch(0)
 sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth())
 self.tableWidget.setSizePolicy(sizePolicy)
 self.tableWidget.setBaseSize(QtCore.QSize(0, 0))
 self.tableWidget.setObjectName("tableWidget")
 self.tableWidget.setColumnCount(5)
 self.tableWidget.setRowCount(0)
 item = QtWidgets.QTableWidgetItem()
 self.tableWidget.setHorizontalHeaderItem(0, item)
 item = QtWidgets.QTableWidgetItem()
 self.tableWidget.setHorizontalHeaderItem(1, item)
 item = QtWidgets.QTableWidgetItem()
 self.tableWidget.setHorizontalHeaderItem(2, item)
 item = QtWidgets.QTableWidgetItem()
 self.tableWidget.setHorizontalHeaderItem(3, item)
 item = QtWidgets.QTableWidgetItem()
 self.tableWidget.setHorizontalHeaderItem(4, item)
 self.tableWidget.horizontalHeader().setDefaultSectionSize(155)
 self.tableWidget.verticalHeader().setDefaultSectionSize(47)
 self.tableWidget.verticalHeader().setMinimumSectionSize(45)
 self.line = QtWidgets.QFrame(self.scrollAreaWidgetContents)
 self.line.setGeometry(QtCore.QRect(0, 23, 784, 31))
 self.line.setToolTipDuration(0)
 self.line.setLineWidth(1)
 self.line.setFrameShape(QtWidgets.QFrame.HLine)
 self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
 self.line.setObjectName("line")
 self.scrollArea.setWidget(self.scrollAreaWidgetContents)
 MainWindow.setCentralWidget(self.centralwidget)

 self.retranslateUi(MainWindow)
 self.pushButton.clicked.connect(lambda :event.search(self.tableWidget, self.lineEdit.text(), self.lineEdit_2.text(), self.dateEdit.date().toPyDate()))
 QtCore.QMetaObject.connectSlotsByName(MainWindow)

 def retranslateUi(self, MainWindow):
 _translate = QtCore.QCoreApplication.translate
 MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
 self.label.setText(_translate("MainWindow", "From"))
 self.label_2.setText(_translate("MainWindow", "To"))
 self.pushButton.setText(_translate("MainWindow", "search"))
 item = self.tableWidget.horizontalHeaderItem(0)
 item.setText(_translate("MainWindow", "車次"))
 item = self.tableWidget.horizontalHeaderItem(1)
 item.setText(_translate("MainWindow", "出發時間"))
 item = self.tableWidget.horizontalHeaderItem(2)
 item.setText(_translate("MainWindow", "到站時間"))
 item = self.tableWidget.horizontalHeaderItem(3)
 item.setText(_translate("MainWindow", "硬臥"))
 item = self.tableWidget.horizontalHeaderItem(4)
 item.setText(_translate("MainWindow", "硬座"))


if __name__ == "__main__":
 import sys
 app = QtWidgets.QApplication(sys.argv)
 MainWindow = QtWidgets.QMainWindow()
 ui = Ui_MainWindow()
 ui.setupUi(MainWindow)
 MainWindow.show()
 sys.exit(app.exec_())

2、EventClass.py(自定義槽函數類)

#coding:u8
import time
from PyQt5.QtWidgets import QTableWidgetItem
from TrainService import TrainService
class EventClass(object):
 def __init__(self):
 self.trainService=TrainService()
 pass
 def search(self, table, From="北京", To="上海", Date=time.strftime("%Y-%m-%d", time.localtime())):
 print("【{}】 form {} to {}".format(Date, From, To))
 train_list=self.trainService.crawlTrainMess(From, To, Date)
 print(table)
 table.setRowCount(len(train_list))
 for row, item in enumerate(train_list):
  for col, i in enumerate(item):
  if i is "":
   i="0"
  table.setItem(row, col, QTableWidgetItem(i))
 pass

3、TrainService.py(爬蟲類)

#coding_url:u8
import requests
import json
class TrainService(object):
 def __init__(self):
 with open("code.json", 'r') as file:
  code=eval(file.read())
 self.code=code
 self.code_url="https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9002"
 self.headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.7 Safari/537.36"}
 
 def crawlTrainMess(self,from_station,to_station,train_date):
 self.url="https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date={}&leftTicketDTO.from_station={}&leftTicketDTO.to_station={}&purpose_codes=ADULT".format(train_date,self.code[from_station],self.code[to_station])
 response=requests.get(self.url,headers=self.headers)
 train_json=json.loads(response.text)
 results=train_json['data']['result']
 train_list=[]
 for i in results:
  temp=i.split('|')
  train_list.append([temp[3], temp[8], temp[9], temp[25], temp[26]])
 #print(train_list)
 return train_list
 def crawlCodeMess(self):
 response=requests.get(self.code_url,headers=self.headers)
 station_list=response.text.split('@')[1 :]
 code={}
 for i in station_list:
  temp=i.split('|')
  code[temp[1]]=temp[2]
 #print(code)
 with open("code.json", 'w') as file:
  file.write(str(code))
 
if __name__=="__main__":
 ts=TrainService()
 ts.crawlCodeMess()

需要先跑 TrainService.py 獲取到站點對應的代碼信息code.json
然后在執行 ui.py

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

尼木县| 巴里| 互助| 额济纳旗| 惠州市| 清水县| 安平县| 海淀区| 白城市| 闸北区| 长宁区| 黔南| 陆良县| 郧西县| 沧州市| 佛冈县| 东阿县| 丰原市| 孟州市| 富平县| 苏尼特右旗| 西峡县| 皮山县| 武平县| 金门县| 始兴县| 伊川县| 乡城县| 赫章县| 甘孜县| 灵武市| 千阳县| 汽车| 呼图壁县| 霍州市| 财经| 堆龙德庆县| 二手房| 五峰| 肇庆市| 行唐县|