欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PyQt5+requests实现车票查询工具

程序员文章站 2023-12-02 17:45:16
pyqt5+requests实现一个车票查询工具,供大家参考,具体内容如下 结构图 效果图 思路 1、search(qpushbutton)点击信号(cli...

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。