#!/usr/bin/env python

#coding=utf8

#12306查票爬虫

import requests,json,sys

#获取地址代码

#https://kyfw.12306.cn/otn/resources/js/framework/favorite_name.js

url = 'https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9025'

url_tmp = str(requests.get(url, verify=False).content, encoding='utf-8').replace("var station_names ='@", '').replace(

"';", '')

url_tmp = list(url_tmp.split('@'))

def dizhi_code(dizhi):

'''

通过站点名字获取code

'''

for i in url_tmp:

if dizhi == i.split('|')[1]:

return i.split('|')[2]

def code_dizhi(code):

'''

通过code获取站点名字

'''

# print (url_tmp)

for i in url_tmp:

if code == i.split('|')[2]:

return i.split('|')[1]

def get_lieche(start_che,end_che,date):

start_che=dizhi_code(start_che)

end_che=dizhi_code(end_che)

# print (start_che,end_che)

try:

url='https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=%s&leftTicketDTO.from_station=%s&leftTicketDTO.to_station=%s&purpose_codes=ADULT' %(date,start_che,end_che)

# print (url)

data = json.loads(requests.get(url, verify=False).content)

except Exception as e:

print("获取数据失败,可能网络错误或者请求太频繁",e)

sys.exit(2)

chuli_data=[]

for i in data['data']['result']:

list_che=list(i.strip('|').split('|'))

if list_che[0]=="23:00-06:00系统维护时间" or list_che[0]=="预订" :

#时间 列车班号 始发站 终点站 始发时间 终点时间 一共时间 商务座 一等座 二等座 软卧 硬卧

chuli_data.append((che_time,list_che[2],code_dizhi(list_che[3]),code_dizhi(list_che[4]),list_che[7],list_che[8],list_che[9],list_che[-4],list_che[-5],list_che[-6],list_che[-13],list_che[-8],list_che[-7]))

else:

chuli_data.append((che_time, list_che[3], code_dizhi(list_che[4]), code_dizhi(list_che[5]), list_che[8],

list_che[9], list_che[10], list_che[-4], list_che[-5], list_che[-6], list_che[-13],list_che[-8],list_che[-7]))

# print (list_che)

# print (chuli_data)

print("时间 \t\t始发站\t终点站\t始发时间\t终点时间\t一共时间\t商务座\t一等座\t二等座\t软卧\t硬卧\t硬座\t列车班号")

for i in chuli_data:

# print (i)

print("%s\t%s\t%s\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s" %(i[0],i[2],i[3],i[4],i[5],i[6],i[7],i[8],i[9],i[10],i[11],i[12],i[1]))

if __name__ == '__main__':

#get_lieche("重庆","北京",'2017-09-06')

start_zhan=input("请输入起始站:")

end_zhan = input("请输入终点站:")

che_time = input("请输入时间范例2017-09-03:")

if start_zhan=="" or end_zhan=="" or che_time=="":

print("站点名错误")

sys.exit(1)

# elif che_time=="":

# che_time=datetime.date()

get_lieche(start_zhan, end_zhan, che_time)

python爬取12306_python爬取12306列车信息相关推荐

  1. 2021最新 python爬取12306列车信息自动抢票并自动识别验证码(三)购票篇

    项目前言 tiebanggg又来更新了,项目--[12306-tiebanggg-master]注:本项目仅供学习研究,如若侵犯到贵公司权益请联系我第一时间进行删除:切忌用于一切非法途径,否则后果自行 ...

  2. 2021最新python爬取12306列车信息自动抢票并自动识别验证码

    项目描述 项目前言 tiebanggg又来更新了,项目--[12306-tiebanggg-master]注:本项目仅供学习研究,如若侵犯到贵公司权益请联系我第一时间进行删除:切忌用于一切非法途径,否 ...

  3. python爬取12306列车信息自动抢票并自动识别验证码(一)列车数据获取篇

    项目前言 自学python差不多有一年半载了,这两天利用在甲方公司搬砖空闲之余写了个小项目--[12306-tiebanggg-master].注:本项目仅供学习研究,如若侵犯到贵公司权益请联系我第一 ...

  4. python爬取12306列车信息自动抢票并自动识别验证码(二)selenium登录验证篇

    项目前言 自学python差不多有一年半载了,这两天利用在甲方公司搬砖空闲之余写了个小项目--[12306-tiebanggg-master]注:本项目仅供学习研究,如若侵犯到贵公司权益请联系我第一时 ...

  5. python爬取12306_Python爬取12306车次信息代码详解

    详情查看下面的代码: 如果被识别就要添加一个cookie如果没有被识别的话就要一个user-agent就好了.如果出现乱码就设置编码格式为utf-8 #静态的数据一般在elements中(复制文字到s ...

  6. python爬取12306_Python 爬取12306火车票

    #!/usr/bin/env python#coding:utf-8 """命令行的火车票查看器 Usage: 12306 [-gdtkz] Options: -h,-- ...

  7. python爬取12306_python爬取12306的车次信息

    #静态的数据一般在elements中(复制文字到sources按ctrl+f搜索.找到的为静态),而动态去network中去寻找相关的信息 import requests import re def ...

  8. python爬取12306_python爬取12306火车车次信息

    #-*- coding:utf-8 -*- importrequestsimportreimportjson#关闭https证书验证警告 requests.packages.urllib3.disab ...

  9. python模拟登录12306_Python模拟登录12306的方法

    #!/usr/bin/python # -*- coding: utf-8 -*- import re; import sys; import cookielib; import urllib; im ...

最新文章

  1. leetcode算法题--调整数组顺序使奇数位于偶数前面
  2. 如何看待 2020 届校招算法岗「爆炸」的情况?英雄所见略同
  3. 【渝粤题库】国家开放大学2021春1070组织行为学题目
  4. Linux将硬盘转化为pv,Linux扩展硬盘 物理卷(PV) 卷组(VG) 逻辑卷(LV)
  5. python选择题题库百度文库_大学Python程序题题库
  6. 重新安装python2.6 和 yum (不可以直接安装yum yum 依赖于python2.6)
  7. 配色没有灵感?最流行的配色案例!没有一个人不爱的
  8. N 层应用程序中的数据检索和 CUD 操作 (LINQ to SQL)
  9. Java枚举类型的用法
  10. 前嗅ForeSpider教程:配置关键词
  11. 工具篇1-CMDER--windows下工具,模拟linux好用的工具
  12. 解决win10cmd乱码
  13. 高通平台开发系列讲解(外设篇)BMI160基本配置
  14. 基于SSM的教师本科教学质量评价管理系统
  15. ArcFace论文翻译
  16. html单元格文字首行缩进,刘启智问:excel里怎么首行缩进 Excel在表格中的文字要首行缩进二字符,该如何设置?...
  17. 广义相对论 的 一个问题
  18. 如何在vmware中安装mac OS系统
  19. 设置MFC对话框背景图片
  20. 冯诺依曼原理奠定了至今仍然在使用的计算机,计算机二级MS-Office真题「选择题」...

热门文章

  1. 密码与安全新技术专题
  2. 给文字上加中划线_Word中为字符添加上划线该怎么做
  3. 数据分析 --- 如何分析数据
  4. 外文翻译 | 你以为你会用Math.random() ? 不,你不会……
  5. 教程篇(7.0) 03. FortiGate基础架构 虚拟域(VDOM) ❀ Fortinet 网络安全专家 NSE 4
  6. Psychopy学习笔记上|online流程,unexpected token
  7. PNAS|助人为乐—助人行为能减轻自身身体疼痛
  8. SQL 为什么动不动就 N 百行以 K 计
  9. windows脚本创建桌面快捷图标方式
  10. 美团网2015秋季校园招聘面试题(下)