(一)Python查询12306余票:实现始发站、终点站和出发日期的合法性检验

项目介绍首页python

一、建立字典info存放查询信息(始发站、终点站、出发日期):web

info = {
    'from_station': '',
    'to_station': '',
    'from_date': ''
}

二、实现检查查询信息的函数:
设计思路:svg

  • 输入的站点是否是在全国现有车站中;
  • 输入的始发站和终点站必须不一样;
  • 输入日期必须在15天内。

在此方法中出现的station*在stationsInfo.py中,能够思考一下为何使用这些变量,一些变量在后续会使用,如今只须要stationLists。函数

#检查输入信息from_station, to_station, d
def inputArgs(from_station, to_station, d):
    now_time = datetime.datetime.now()  # 当前日期
    # 校验
    flag1 = False
    flag2 = False
    flag3 = False

    while flag1 == False or flag2 == False or flag3 == False:
        #输入的站点是否是在全国现有的车站列表中,若是有,使用count后返回>0的值
        from_index = stationLists.count(from_station)
        to_index = stationLists.count(to_station)
        # 始发站在车站列表中,而且始发站和终点站不一样
        if from_index > 0 and to_station != from_station:
            flag1 = True
        # 终点站在车站列表中,而且始发站和终点站不一样
        if to_index > 0 and to_station != from_station:
            flag2 = True
        rdate = re.match(r'^(\d{4})-(\d{2})-(\d{2})$', d)
        if rdate:
            from_date = datetime.datetime.strptime(d, '%Y-%m-%d')
            sub_day = (from_date - now_time).days
            if -1 <= sub_day < 15:
                flag3 = True
        if not flag1:
            print("始发站不合法!")
            from_station = input("请输入出发站(年-月-日):\n")
        if not flag2:
            print("终点站不合法!")
            to_station = input("请输入目的地:\n")
        if not flag3:
            print("出发日期不合法!")
            d = input("请输入出发日期(格式:年-月-日):\n")
            from_date = datetime.datetime.strptime(d, '%Y-%m-%d')
            sub_day = (from_date - now_time).days
    info['from_station'] = from_station
    info['to_station'] = to_station
    info['from_date'] = d
    return info

三、测试代码:测试

import datetime
import re
from stationsInfo import stationLists
info = {
    'from_station': '',
    'to_station': '',
    'from_date': ''
}

#检查输入信息from_station, to_station, d
def inputArgs(from_station, to_station, d):
    # 输入车站信息
    # from_station = input("请输入出发站:\n")
    # to_station = input("请输入目的地:\n")
    # d = input("请输入出发日期(格式:年-月-日):\n")
    now_time = datetime.datetime.now()  # 当前日期
    # 校验
    flag1 = False
    flag2 = False
    flag3 = False

    while flag1 == False or flag2 == False or flag3 == False:
        from_index = stationLists.count(from_station)
        to_index = stationLists.count(to_station)
        # 始发站在车站列表中,而且始发站和终点站不一样
        if from_index > 0 and to_station != from_station:
            flag1 = True
        # 终点站在车站列表中,而且始发站和终点站不一样
        if to_index > 0 and to_station != from_station:
            flag2 = True
        rdate = re.match(r'^(\d{4})-(\d{2})-(\d{2})$', d)
        if rdate:
            from_date = datetime.datetime.strptime(d, '%Y-%m-%d')
            sub_day = (from_date - now_time).days
            if -1 <= sub_day < 15:
                flag3 = True
        if not flag1:
            print("始发站不合法!")
            from_station = input("请输入出发站:\n")
        if not flag2:
            print("终点站不合法!")
            to_station = input("请输入目的地:\n")
        if not flag3:
            print("出发日期不合法!")
            d = input("请输入出发日期(格式:年-月-日):\n")
            from_date = datetime.datetime.strptime(d, '%Y-%m-%d')
            sub_day = (from_date - now_time).days
    info['from_station'] = from_station
    info['to_station'] = to_station
    info['from_date'] = d
    return info

if __name__ == "__main__":#main方法
    isContionue = 'Y'

    while isContionue == 'Y' or isContionue == 'y':
        from_stations = input('请输入始发站:\n')
        to_statsions = input('请输入终点站:\n')
        from_date = input('请输入出发日期:\n')
        info = inputArgs(from_stations, to_statsions, from_date)
        print(info)
        isContionue = input('是否继续?Y/N\n')
    input('按任意键退出...')

这里写图片描述

成功检测出错误的输入参数。
注意:输入的始发站和终点站要符合形式相同的原则,即都是拼音或都是中文或都是简称。spa

下一篇–>联网获取余票信息并解析.net