1、登陸官網,找到所需票頁面:
2、按F12然後點擊查詢:
3、先安裝requests 第三方庫:
#pip install requests
4、寫入以下代碼:
1 #!/usr/bin/env python3 2 # -*- coding: UTF-8 -*- 3 4 import requests 5 6 #獲取12306官網的數據url 7 def pacong(): 8 reque = requests.get(‘https://kyfw.12306.cn/otn/leftTicket/queryX?leftTicketDTO.train_date=2019-03-09&leftTicketDTO.from_station=HZH&leftTicketDTO.to_station=BJP&purpose_codes=ADULT‘) 9 10 return reque.json()[‘data‘][‘result‘] 11 12 for i in pacong(): 13 tem_list = i.split(‘|‘) 14 15 if tem_list[32] != ‘‘ and tem_list[32] != ‘无‘: 16 print("有", tem_list[3], "二等座\t", "出发时间:", tem_list[8], "到达时间:\t", tem_list[9], tem_list[32]) 17 else: 18 print("无") 19 """ 20 #查看需要的信息所在的下標 21 print (tem_list) 22 p = 0 23 for n in tem_list: 24 print (p, n) 25 p += 1 26 """
5、結果:
原文地址:https://www.cnblogs.com/hui-shao/p/python_12306.html
时间: 2024-11-05 22:32:17