#!/usr/bin/python2.7 # -*- coding:utf-8 -*- # 2017-10-19 """ 12306 抢票 网页分析 - 查看网页源码 - 判断是否通过异步加载或者框架的形式接收并展示数据 - 数据加密过,然后前端再通过js解密并展示(即js混淆) urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)> 证书作用:保证数据在传输的过程中不会被篡改或截取 """ import sys reload(sys) sys.setdefaultencoding('utf8') import ssl import json import urllib2 # 跳过证书验证 ssl._create_default_https_context = ssl._create_unverified_context headers = { 'Referer':'https://kyfw.12306.cn/otn/leftTicket/init', 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36' } def getTrainList(): url = "https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2017-10-20&leftTicketDTO.from_station=BJP&leftTicketDTO.to_station=CDW&purpose_codes=ADULT" req = urllib2.Request(url) req.headers = headers html = urllib2.urlopen(req).read() res = json.loads(html) return res['data']['result'] if __name__ == '__main__': c = 0 for i in getTrainList(): tmp_list = i.split('|') # print "%s %s %s" % (tmp_list[13],tmp_list[23],tmp_list[3]) if tmp_list[23] == u'有': print "时间:%s 车次:%s 软卧有很多票..." % (tmp_list[13],tmp_list[3]) elif tmp_list[23] == u'无' or not tmp_list[23]: print "时间:%s 车次:%s 软卧无票..." % (tmp_list[13],tmp_list[3]) elif int(tmp_list[23]) > 0: print "时间:%s 车次:%s 软卧有 %s 张票" % (tmp_list[13],tmp_list[3],tmp_list[23]) else: print "时间:%s 车次:%s 软卧无票..." % (tmp_list[13], tmp_list[3])