zoukankan      html  css  js  c++  java
  • Python-12306车票查询

    个人训练需要类似这种从json提取多个信息

    写完后发现可以满足自己查询车票的需求,所以加上了输入查询的功能

    上代码:

    import requests
    import json
    import re
    
    url = 'https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2019-05-09&leftTicketDTO.from_station=CQW&leftTicketDTO.to_station=GIW&purpose_codes=ADULT'
    
    # 获取地名对应的代号
    def get_name(name,headers):
        url = 'https://www.12306.cn/index/script/core/common/station_name_v10028.js'
        response = requests.get(url,headers = headers)
        res = re.findall(r'@.+?|(.+?)|(.+?)|',response.text)
        for i in res:
            if i[0] == name:
                return i[1]
        return None
    
    # 将信息中的' '值转化为网页类似的那种'--'
    def clear_info(list):
        for i in range(len(list)):
            if list[i] == '':
                list[i] = '--'
        return list
    
    # 获取网页信息
    def get_html(beg_name,st_name,time):
        url = 'https://kyfw.12306.cn/otn/leftTicket/query'
    
        params = {
            'leftTicketDTO.train_date': time,
            'leftTicketDTO.from_station': beg_name,
            'leftTicketDTO.to_station': st_name,
            'purpose_codes': 'ADULT'
        }
    
        response = requests.get(url, headers=headers, params=params)
        return response.text
    
    headers = {
        'Cookie': 'JSESSIONID=696CD9155D2B8006B7881933C8CA25E8; _jc_save_wfdc_flag=dc; BIGipServerotn=921698826.50210.0000; RAIL_EXPIRATION=1557715740051; RAIL_DEVICEID=miUOG_8cGgit72oTyiFjYm7fe2HzLle69Ud1Pf7HgG-T9d8t7RkC1iBe3m0aLgz8Vtb-DOp-rsEWVoxAazt4euSyRznwBRGpnLLdja51hvo2cFeIGTK5ON8mMLldB_ZH3Hx3yXe5OCL_hcON4XpV3bmQt4VYNnd4; BIGipServerpool_passport=267190794.50215.0000; route=6f50b51faa11b987e576cdb301e545c4; _jc_save_fromDate=2019-05-09; _jc_save_toDate=2019-05-09; SL_GWPT_Show_Hide_tmp=1; SL_wptGlobTipTmp=1; _jc_save_fromStation=%u91CD%u5E86%2CCQW; _jc_save_toStation=%u8D35%u9633%2CGIW',
        'Host': 'kyfw.12306.cn',
        'If-Modified-Since': '0',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest'
    }
    
    
    beg_name = input("请输入出发点:")
    st_name = input("请输入到站点:")
    time = input("请输入启程时间:")
    
    beg_name = get_name(beg_name,headers)
    st_name = get_name(st_name,headers)
    if beg_name == None or beg_name == None:
        blo_ch = False
        print("输入地点有错")
    if blo_ch:
        # 访问网站并处理json数据
        response = get_html(beg_name, st_name, time)
        response = json.loads(response)
        adder = response['data']['map']
        res = response['data']['result']
    
        # 将需要的信息提出来
        for item in res:
            info = []
            item = item.split('|')
            info.append(item[3])
            info.append(adder[item[6]])
            info.append(adder[item[7]])
            info.append(item[8])
            info.append(item[9])
            info.append(item[10])
            info.append(item[32])
            info.append(item[31])
            info.append(item[30])
            info.append(item[23])
            info.append(item[28])
            info.append(item[29])
            info.append(item[26])
            last_info = clear_info(info)
            print(last_info)

    代码简单  没事练练手

  • 相关阅读:
    政府信息化建设重点——服务、多元化
    随便聊聊水面效果的2D实现(一)
    【Oracel 基础】小结
    漫话Unity(二)
    Codeforces Round #265 (Div. 2) C. No to Palindromes!
    C99中的restrict和C89的volatilekeyword
    开源 java CMS
    JavaScript--基于对象的脚本语言学习笔记(二)
    小试“以图搜图”
    计算几何 《模板》
  • 原文地址:https://www.cnblogs.com/MaGnet/p/10843819.html
Copyright © 2011-2022 走看看