1.本来想写一个自动抢票的脚本,但是在预订时,有一个参数不知道如何构造,因而只能半途而废,在网上查了很久,发现都是用一些自动化测试工具(selenium,Splinter)写的。
下面贴一下我的前面的代码
1 # coding = utf-8 2 from urllib.parse import urlencode 3 4 import requests 5 from .settings import username, password, get_answer 6 session = requests.Session() 7 # 1.获取cookie 8 start_url = "https://kyfw.12306.cn/otn/login/init" 9 session.get(start_url) 10 # 2.获取验证码 11 captcha_url = "https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand&0.11248564942476502" 12 response = session.get(captcha_url) 13 with open('captcha.png', 'wb') as fp: 14 fp.write(response.content) 15 # 3.校验验证码 16 check_captcha_url = "https://kyfw.12306.cn/passport/captcha/captcha-check" 17 data = { 18 "answer": get_answer(), 19 "login_site": "E", 20 "rand": "sjrand" 21 } 22 response = session.post(check_captcha_url, data=data) 23 print(response.text) 24 # 4.校验用户名和密码 25 login_url = "https://kyfw.12306.cn/passport/web/login" 26 data = { 27 "username": username, 28 "password": password, 29 "appid": "otn" 30 } 31 response = session.post(login_url, data=data) 32 print(response.text) 33 # 5.查询车票 34 base_select_url = "https://kyfw.12306.cn/otn/leftTicket/query?" 35 params = { 36 "leftTicketDTO.train_date": "", 37 "leftTicketDTO.from_station": "", 38 "leftTicketDTO.to_station": "", 39 "purpose_codes=ADULT": "" 40 } 41 select_url = base_select_url + urlencode(params) 42 session.get(select_url) 43 "secretStr=BgGg3ahcOjLqIBDMSHMPMAZRSRIGj%2FKUMlb0d5D6Jf3XcC1FrHl3iW30%2FMelzJ0DX4WhejtUM9yH%0AdmgLn6X6nIC8LWdoTjml6ZuXtQeCDOhBRfhml2ePFiGYHZdZ1m5CzXy9h34OdONVz6K%2FqIBnvkhd%0AGEq0lsFEZ8gd0nEAEEmGeeh3gGQo58Fw%2FRKHAQeVMUtdq9vfM3TJ4exOw9glQh1JgfPJVIo%2FADlh%0Av1d4kUcgRnDZqTCltaGG9GmC9eAF3dyY66VojPI%3D&train_date=2018-08-10& 44 back_train_date=2018-07-26& 45 tour_flag=dc&purpose_codes=ADULT& 46 query_from_station_name=XFN& 47 query_to_station_name=WHN& 48 undefined"