6
回答
爬虫12306的查票系统,得到的json如何解析?
终于搞明白,存储TCO原来是这样算的>>>   

#-*- coding: utf-8 -*-
import requests
global GET_INFO
from urllib import request
import json
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
global GET_INFO
req_url = 'https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2017-05-03&leftTicketDTO.from_station=EAY&leftTicketDTO.to_station=IOQ&purpose_codes=ADULT'
with request.urlopen(req_url) as f:
    GET_INFO = f.read()
    print('Status:', f.status, f.reason)
    print (type(GET_INFO))
    m=GET_INFO.decode()
    #d1 = json.dumps(r2.json(),sort_keys=True,ensure_ascii=False,indent=4)
    data = json.loads(m, encoding='UTF-8')
    print (data)

 

得出这样的json结果:

Status: 200 OK
<class 'bytes'>
{'status': True, 'httpstatus': 200, 'validateMessages': {}, 'messages': [], 'validateMessagesShowId': '_validatorMessage', 'data': {'flag': '1', 'result': ['mqwMPl6KWCs2nqEfn7ExhiTr%2BZUMfaJRKE9qiiM37EVeYrdae5cklhAXOqWn4t4hM2xYoyXgmBqA%0AgcySLxr1rf2YQ6o%2FG6ZuYSJmkL5%2F0mvoIGZU07D7HFru%2Fcy%2F%2FYl1tp89NcCKqSfFIsWwubeMsp6%2F%0Ar6JaWOhPNXj2YuW1U4bNhNb7FL2F2d%2FNgc1%2FRySRHx32U1BVo8bczf%2FNYYqqaCtyqcfkNbBvcJdJ%0AS%2FMv%2B6U%3D|预订|4f0000G8200A|G820|EAY|IOQ|EAY|IOQ|09:42|19:09|09:27|Y|tdYC0UpS7RFeygf8bGeDr2dHmrY9R0YiptQ37kwOtRqJIOxR|20170503|3|Y2|01|15|1|0||||||8|||||有|12||O0M0P0|OMP', 'Zv4Wn03S8jWsP8B%2BE26EUnKnU1JLhdeup3kmFPHwJ8DEGQ9wHwrIohIL35AU24QVdqFFrQkZcdP3%0APYf489kXoFInqw%2Fgd0%2B2zu%2Bxso%2FTb%2BwoPYjOJnG72A3h2UpqpF%2FlGw%2FioTZSdaRHuZmBvFJt4m%2Fx%0A4xRotZ4v7utlkNjsKfq17yH9srRGp4Pbft3CQPtrkHLKqy5PKTcHXvv9ISoCJox4ToZ9GfPyDNSn%0AgAGRaew%3D|预订|4f0000G8240F|G824|EAY|IOQ|EAY|IOQ|11:00|20:30|09:30|Y|SxtkCpyxpMYRFRQpNaVtzdVPQcOmgsh8cOaTUdWikra1Ji6C|20170503|3|Y2|01|17|1|0|||||||||||有|无|9|O0M090|OM9', '|预订|4f0000G8280L|G828|EAY|IOQ|EAY|IOQ|12:37|22:20|09:43|N|mF97vweKdYCU4wV6tc1ZIY9C3EJ7LDN7|20170503|3|Y2|01|15|1|0|||||||||||无|无||O0M0|OM', '4WtGDi%2B%2Bmv%2FmGBKQiekGliOgs%2FdHHPsCvL8z3gpZ2md0p97H75HRZ%2FrYAQ3IRLGPH3xBcwrwB9wV%0AsIKgrxioW39XQ0%2B6gEcxWaSQEeIGFYyDlxPHJvhiXPlHX%2F0RvSiII2qt8OmxgIVhHh45O57kNICY%0AbWrXevUJatgAu2tduubGtNcSbZKamWYycXE%2F5Eqk0OHQKYYQWZES5vettt%2FHROpouQKWOOq9Uq5A%0Ang%3D%3D|预订|930000Z23201|Z232|WAR|SZQ|XAY|SZQ|19:49|19:28|23:39|Y|v03Sdi0AfAnvV8o%2BuRShi4Y%2BsyLggKFS6IMuqxHkPghzU2ov|20170502|3|R1|12|22|0|0|||||||无||有|有||||101030|113', 'mwh%2BZ2ArxaoYKZ7O8WjfrroTtUp%2FyaZ2YO9Wzztr%2BHFjzNC%2BrCDJ4h%2BPbX6H5dfKJwjGUYx%2B126U%0A78GDNQgmkCPy0vFIb9jRqTPO100WC9pw7UdbZUeTpMBpqrcKcgT4mM%2FJ72ellIteykcpz4g9yId0%0AwcHy5nmMil3m2DPEDy4FWBuhfEYcOpT7XdSoxbiGdkF2Y815xItI5ejI3agX8kWpDVG%2BPJMeW9vM%0Akw5vzZfEJ0L8|预订|410000K4480M|K448|XAY|SZQ|XAY|SZQ|23:20|04:36|29:16|Y|yhi5ZvC7ao%2FpVJUImWd0q4ZZGWzXXz9zmXwSf1v0rHuWxmwul3Ydp1%2FNKM8%3D|20170503|3|Y2|01|24|0|0||||无|||有||有|有||||10401030|1413', 'YiPUpUkJkZCKgjoM%2BjUUuiC%2Fuo9yOqxwKskZPfrQJS8p6lX13gnGEVkpsWOclBnMzkspVTAatVfQ%0AHo2FSiOH8fPh4xHNlAUG%2B5jDulOM96MYpLPU7lZkGEzQ9FaoqAk%2BYELuB6lDSiS%2FuruFUZLmnAQo%0AYSsiBPoojDrITj%2FOPPkxMihRtxtiWM49WpPerFTxT8opLmUkSvgKNXQZS8GyaGYVuJNR2Tr1tq0A%0ABxvvP7XQBO%2FyWFgwkw%3D%3D|预订|850000K13202|K132|LZJ|OSQ|XAY|OSQ|23:40|09:30|33:50|Y|AXossqm9zGIn2bg4t32I4x0mGQvxZg8DoTfAbTkcmNsoToCAwUvdDiJj59A%3D|20170503|3|J1|08|34|0|0||||无|||无||19|11||||10401030|1413'], 'map': {'EAY': '西安北', 'XAY': '西安', 'SZQ': '深圳', 'OSQ': '深圳西', 'IOQ': '深圳北'}}}
 

尝试了很多方式,得到的json均与其它人描述的不太一样。data数据是什么格式,如何解析???谢谢

顶部