Python implementation 2014 train ticket query code sharing

Source: Internet
Author: User
Tags ticket
Code based on python3.3.3,pyqt5.1.1

Copy the Code code as follows:


#-*-Coding:utf-8-*-
# Python 3.3.3
# PyQt 5.1.1
Import Sys,time,re,urllib.parse,urllib.request,http.cookiejar,json
From Pyqt5.qtcore Import *
From Pyqt5.qtgui Import *
From pyqt5.qtwidgets Import *

"" "Cookie" "" "
Cookie=http.cookiejar.lwpcookiejar ()
#cookie. Load (' F:/cookie.txt ', true,true)
Chandle=urllib.request.httpcookieprocessor (Cookie)

"" "Get Data" ""
def getData (URL):
R=urllib.request.request (URL)
Opener=urllib.request.build_opener (CHandle)
U=opener.open (R)
#chandle. Cookiejar.save (' F:/cookie.txt ', true,true)
Data=u.read ()
Try
Data=data.decode (' Utf-8 ')
Except
Data=data.decode (' GBK ', ' ignore ')
Return data
def postdata (Url,data):
Data=urllib.parse.urlencode (data);d ata=bytes (data, ' utf-8 ')
R=urllib.request.request (Url,data)
Opener=urllib.request.build_opener (CHandle)
U=opener.open (R)
#chandle. Cookiejar.save (' F:/cookie.txt ', true,true)
Data=u.read ()
Try
Data=data.decode (' Utf-8 ')
Except
Data=data.decode (' GBK ', ' ignore ')
Return data
"" "Train Ticket" ""
Class Ticket:
def init (self,s,e,d):
Self.li=[]
Cont=getdata (' Https://kyfw.12306.cn/otn/resources/js/framework/station_name.js ')
S=re.findall ('%s\| ( [^|] +) '% S,cont) [0]
E=re.findall ('%s\| ( [^|] +) '% E,cont) [0]
Url= ' Https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=0X00&queryDate=%s&from_station=%s&to_ station=%s '% (d,s,e)
Cont=json.loads (getData (URL)) ["Data"] ["datas"]
name=[
"Station_train_code",
"From_station_name",
"To_station_name",
"Lishi",
"Swz_num",
"Tz_num",
"Zy_num",
"Ze_num",
"Gr_num",
"Rw_num",
"Yw_num",
"Rz_num",
"Yz_num",
"Wz_num",
"Qt_num"
]
For x in cont:
Tmp=[]
For y in Name:
If y== "From_station_name":
s=x[y]+ ' \ n ' +x["start_time"]
Tmp.append (s)
Elif y== "To_station_name":
s=x[y]+ ' \ n ' +x["Arrive_time"]
Tmp.append (s)
Else
Tmp.append (X[y])
Self.li.append (TMP)
"" UI "" "
Class Dialog (Qdialog):
Ticket=ticket ()
def __init__ (self):
Super (). __INIT__ ()
Self.resize (750,350)
#布局管理器
Self.layout=[qvboxlayout (self), qhboxlayout ()]
Self.layout[1].setcontentsmargins (0,0,0,0)
Self.layout[1].setspacing (0)
Self.layout[0].setcontentsmargins (0,0,0,0)
Self.layout[0].setspacing (0)
Self.layout[0].addlayout (Self.layout[1])
#按钮
Btn=qpushbutton ("OK")
Btn.clicked.connect (Self.submit)
#输入选项
Label=[qlabel ("FA Station:"), Qlabel ("Arrival:"), Qlabel ("Date:")]
Self.line=[qlineedit (), Qlineedit (), Qcombobox ()]
Y=int (Time.strftime ("%Y", Time.localtime ()))
M=int (Time.strftime ("%m", Time.localtime ()))
D=int (Time.strftime ("%d", Time.localtime ()))
I=0
Yy=y
Mm=m
Dd=d
While i<20:
If M in (1,3,5,7,8,10,12):
If d+i>31:
Dd=d+i-31
Mm=m+1
If mm>12:
Yy=y+1
Mm=mm-12
Else
Dd=d+i
Elif m in (4,6,9,11):
If d+i>30:
Dd=d+i-30
Mm=m+1
If mm>12:
Yy=y+1
Mm=mm-12
Else
Dd=d+i
Else
if (m%400==0) or ((m%4==0) and (m%100!=0)):
If d+i>29:
dd=d+i-29
Mm=m+1
If mm>12:
Yy=y+1
Mm=mm-12
Else
Dd=d+i
Else
If d+i>28:
Dd=d+i-28
Mm=m+1
If mm>12:
Yy=y+1
Mm=mm-12
Else
Dd=d+i
s= '%d-%02d-%02d '% (YY,MM,DD)
Self.line[2].additem (s)
I+=1
I=0
While i<3:
Self.line[i].setmaximumwidth (100)
Label[i].setmaximumwidth (50)
Label[i].setalignment (qt.alignright| Qt.alignvcenter)
Self.layout[1].addwidget (Label[i],qt.alignright)
Self.layout[1].addwidget (Self.line[i],qt.alignleft)
I+=1
Self.layout[1].addwidget (BTN)
#表格
head=[' train ', ' Station ', ' Stop ', ' duration ', ' Business seat ', ' principal seat ', ' premier seat ', ' second seat ', ' advanced soft sleeper ', ' soft sleeper ', ' hard sleeper ', ' soft ', ' hard seat ', ' no seats ', ' other ']
Self.table=qtablewidget ()
Self.table.setEditTriggers (Qabstractitemview.noedittriggers)
Self.table.setColumnCount (15)
Self.table.setHorizontalHeaderLabels (head)
Self.layout[0].addwidget (self.table)
Self.show ()
def submit (self):
Self.table.clearContents ()
S=self.line[0].text ()
E=self.line[1].text ()
D=self.line[2].currenttext ()
Self.ticket.init (S,E,D)
Self.table.setRowCount (Len (self.ticket.li))
I=0
For x in Self.ticket.li:
J=0
For y in x:
If J==1 or j==2:
Item=qlabel (y)
Item.setalignment (Qt.aligncenter)
Self.table.setCellWidget (I,j,item)
Else
Item=qtablewidgetitem (y)
Item.settextalignment (Qt.aligncenter)
If not re.search (' \d ', y):
Item.setforeground (Qbrush (qt.red))
Self.table.setItem (I,j,item)
If J>2 or j==0:
Self.table.resizeColumnToContents (j)
J+=1
I+=1
If __name__== "__main__":
App=qapplication (SYS.ARGV)
Dialog=dialog ()
Sys.exit (App.exec_ ())

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.