这星期刚开学也是迷迷糊糊的乱搞了一星期,python就是好用,不说废话了,上代码
class hhSprider:
def Ticketquery(self,date,start,end):
self.url = 'https://kyfw.12306.cn/otn/leftTicket/queryT?leftTicketDTO.train_date=%s&leftTicketDTO.from_station=%s&leftTicketDTO.to_station=%s&purpose_codes=ADULT' %(date,start,end)
self.headers = {
"Accept":"text/html,application/xhtml+xml,application/xml;",
"Accept-Encoding":"gzip",
"Accept-Language":"zh-CN,zh;q=0.8",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
}
self.TicketSession = requests.Session()
self.TicketSession.headers = self.headers
self.TicketSession.verify = False
self.getjson = self.TicketSession.get(self.url)
#print self.getjson.content.decode('utf-8').encode('mbcs')
self.data = json.loads(self.getjson.content)
self.left_ticket = self.data['data']
return self.left_ticket
def station_name(self,name):
js=requests.get('https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.8964',verify=False)
#print js.content.decode('utf-8').encode('mbcs')
content = js.content.decode('utf-8').encode('mbcs')
pattern = re.compile(name+'\|(?P[A-Z]{3})\|')
t = re.search(pattern,content)
if t:
print t.groups()[0]
else:
print 'hh'
return t.groups()[0]
def main():
tmp = hhSprider()
ss = raw_input('输入出发地'.decode('utf-8').encode('mbcs'))
ee = raw_input('输入目的地'.decode('utf-8').encode('mbcs'))
tt = raw_input('输入出发日期格式为(2016-09-06)'.decode('utf-8').encode('mbcs'))
start = tmp.station_name(ss)
end = tmp.station_name(ee)
res= tmp.Ticketquery(tt,start,end)
for i in res:
print u'车次:%s' %i['queryLeftNewDTO']['station_train_code'],u' 出发时间:%s'%i['queryLeftNewDTO']['start_time'],u' 到达时间:%s'%i['queryLeftNewDTO']['arrive_time'],u' 硬座:%s'%i['queryLeftNewDTO']['yz_num']
main()
ps:抓接口懵b了多长时间....