python实现登录12306网站查看火车票信息
python实现登录12306网站查看火车票信息
前言
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。
作者:TM0831
送资料python学习群:⑹⑨⑸①⑻⑤⑷②⑼,先:到ā先「得←
具体步骤
一、登录
登录功能是通过使用selenium实现的,用到了超级鹰来识别验证码。没有超级鹰账号的先注册一个账号,充值一点题分,然后把下载这个Python接口文件,再在里面添加一个use_cjy的函数,以后使用的时候传入文件名就可以了(验证码类型和价格可以在价格体系查看):
1 2 3 4 5 6 7 |
<span class="hljs-number">1</span> def use_cjy(filename): <span class="hljs-number">2</span> username = <span class="hljs-string">""</span> # 用户名 <span class="hljs-number">3</span> password = <span class="hljs-string">""</span> # 密码 <span class="hljs-number">4</span> app_id = <span class="hljs-string">""</span> # 软件ID 5 cjy = CJYClient(username, password, app_id) # 用户中心>>软件ID6 im = <span class="hljs-keyword">open</span>(filename, <span class="hljs-string">'rb'</span>).read() # 本地图片文件路径 <span class="hljs-number">7</span> <span class="hljs-keyword">return</span> cjy.PostPic(im, <span class="hljs-number">9004</span>) # <span class="hljs-number">9004</span>->验证码类型 |
然后进入12306的登录页面,网址为https://kyfw.12306.cn/otn/login/init,可以看到有一个像下面这样的验证码:
要破解这个验证码,第一个问题是怎么得到这个验证码图片,我们可以很轻松的找到这个验证码图片的链接,但是如果用requests去请求这个链接,然后把图片下载下来,这样得到的图片和网页上的验证码图片是不同的,因为每次请求都会刷新一次验证码。所以需要换个思路,比如先把网页截个图,然后我们可以知道验证码图片在网页中的位置,然后再根据这个位置,把截图相应的位置给截取出来,就相当于把验证码图片从整个截图中给抠出来了,这样得到的验证码图片就和网页上的验证码一样了。相关代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="hljs-number">1</span> # 定位到验证码图片 <span class="hljs-number">2</span> captcha_img = browser.find_element_by_xpath(<span class="hljs-string">'//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img'</span>) <span class="hljs-number">3</span> location = captcha_img.location <span class="hljs-number">4</span> size = captcha_img.size <span class="hljs-number">5</span> # 写成我们需要截取的位置坐标 <span class="hljs-number">6</span> coordinates = (<span class="hljs-keyword">int</span>(location[<span class="hljs-string">'x'</span>]), <span class="hljs-keyword">int</span>(location[<span class="hljs-string">'y'</span>]), <span class="hljs-number">7</span> <span class="hljs-keyword">int</span>(location[<span class="hljs-string">'x'</span>] + size[<span class="hljs-string">'width'</span>]), <span class="hljs-keyword">int</span>(location[<span class="hljs-string">'y'</span>] + size[<span class="hljs-string">'height'</span>])) <span class="hljs-number">8</span> browser.save_screenshot(<span class="hljs-string">'screen.png'</span>) <span class="hljs-number">9</span> i = Image.open(<span class="hljs-string">'screen.png'</span>) <span class="hljs-number">10</span> # 使用Image的crop函数,从截图中再次截取我们需要的区域 <span class="hljs-number">11</span> verify_code_image = i.crop(coordinates) <span class="hljs-number">12</span> verify_code_image.save(<span class="hljs-string">'captcha.png'</span>) |
现在已经得到了验证码图片了,下一个问题是怎么识别?点触验证码识别起来有两个难点,一个是文字识别,要把图上的鞭炮文字识别出来,第二点是识别图片中的内容,比如上图就要把有鞭炮的图片识别出来,而这两个难点利用OCR技术都很那实现,因此选择使用打码平台(比如超级鹰)来识别验证码。对于上面这个图,在使用超级鹰识别之后会返回下面这个结果:
1 2 |
{<span class="hljs-string">'pic_id'</span>: <span class="hljs-string">'6048511471893900001'</span>, <span class="hljs-string">'err_no'</span>: 0, <span class="hljs-string">'err_str'</span>: <span class="hljs-string">'OK'</span>, <span class="hljs-string">'md5'</span>: <span class="hljs-string">'bde1de3b886fe2019a252934874c6669'</span>, <span class="hljs-string">'pic_str'</span>: <span class="hljs-string">'117,140'</span>} |
其中pic_str对应的值就是有鞭炮的图片的坐标位置(如果有多个坐标,会用“|”进行分隔),我们对这个结果进行解析,把坐标提取出来,再利用selenium模拟点击就可以了,相关代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span class="hljs-number">1</span> # 调用超级鹰识别验证码 <span class="hljs-number">2</span> capture_result = use_cjy(<span class="hljs-string">'captcha.png'</span>) <span class="hljs-number">3</span> <span class="hljs-built_in">print</span>(capture_result) <span class="hljs-number">4</span> # 对返回的结果进行解析 <span class="hljs-number">5</span> groups = capture_result.get(<span class="hljs-string">"pic_str"</span>).split(<span class="hljs-string">'|'</span>) <span class="hljs-number">6</span> points = [[<span class="hljs-keyword">int</span>(number) <span class="hljs-keyword">for</span> number in group.split(<span class="hljs-string">','</span>)] <span class="hljs-keyword">for</span> group in groups] <span class="hljs-number">7</span> <span class="hljs-keyword">for</span> point in points: <span class="hljs-number">8</span> # 先定位到验证图片 <span class="hljs-number">9</span> element = WebDriverWait(browser, <span class="hljs-number">20</span>).until( <span class="hljs-number">10</span> EC.presence_of_element_located((By.CLASS_NAME, <span class="hljs-string">"touclick-bgimg"</span>))) <span class="hljs-number">11</span> # 模拟点击验证图片 <span class="hljs-number">12</span> ActionChains(browser).move_to_element_with_offset(element, point[<span class="hljs-number">0</span>], point[<span class="hljs-number">1</span>]).click().perform() <span class="hljs-number">13</span> sleep(<span class="hljs-number">1</span>) |
二、查询
带有车票信息的ajax接口很容易找到,格式也是标准的json格式,解析起来会方便不少
但是爆保存车票的字符串很复杂,我们先把第一条信息打印出来看看,以下是部分信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span class="hljs-string">'hH0qeKPBgl0X0aCnrtZFyBgzqydzV45U2M1r%2F32FsaPHeb7Mul00sIb7y9W%2B6df1tUdDGCxqdVs8%0Aw2VodSjdXjUQ2uNdwFprKdVK9iaW60Wj2jKpNKaViR4ndlBCjsYB0SIF</span> <span class="hljs-string">QR0pLksy7HDP0KcaoLe4%0A4RW6zRcscO7SRNJZOsF%2Fxj3Ooq76lzzdku3Uw957yjLFyf7ikixOaC%2FAOrLAwCc7y0krRpKJbSn3%0ApBsY%2F%2Fok%2Bmg2xNhXapoCPIt4w0p9'</span>, 这段字符是随机生成的,过几秒就回失效。 <span class="hljs-string">'39000D30280G'</span>, 列车编号 <span class="hljs-string">'D3028'</span>, 车次 <span class="hljs-string">'HKN'</span>, 始发站 <span class="hljs-string">'AOH'</span>, 终点站 <span class="hljs-string">'HKN'</span>, 出发站 <span class="hljs-string">'AOH'</span>, 目的站 <span class="hljs-string">'07:31'</span>, 出发时间 <span class="hljs-string">'13:06'</span>, 到达时间 <span class="hljs-string">'05:35'</span>, 总耗时 <span class="hljs-string">'Y'</span>, Y表示可以购票,N表示不可以 <span class="hljs-string">'20181111'</span>, 日期 后面基本都是座位的余票信息了。 |
对于提到的列车站点代码,可以通过请求这个链接,通过得到JS脚本中的station_names变量获取,对应的站点以@字符分隔,相关代码如下:
1 2 3 4 5 |
<span class="hljs-number">1</span> # 请求保存列车站点代码的链接 <span class="hljs-number">2</span> res1 = requests.<span class="hljs-keyword">get</span>(<span class="hljs-string">"https://kyfw.12306.cn/otn/resources/js/framework/station_name.js"</span>) <span class="hljs-number">3</span> # 把分割处理后的车站信息保存在station_data中 <span class="hljs-number">4</span> <span class="hljs-keyword">self</span>.station_data = res1.text.lstrip(<span class="hljs-string">"var station_names ='"</span>).rstrip(<span class="hljs-string">"'"</span>).<span class="hljs-built_in">split</span>('@') |
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="hljs-selector-tag">1</span> # 返回车站英文缩写 <span class="hljs-selector-tag">2</span> <span class="hljs-selector-tag">def</span> <span class="hljs-selector-tag">get_station</span>(self, city): <span class="hljs-selector-tag">3</span> <span class="hljs-selector-tag">for</span> <span class="hljs-selector-tag">i</span> <span class="hljs-selector-tag">in</span> <span class="hljs-selector-tag">self</span><span class="hljs-selector-class">.station_data</span>: <span class="hljs-selector-tag">4</span> <span class="hljs-selector-tag">if</span> <span class="hljs-selector-tag">city</span> <span class="hljs-selector-tag">in</span> <span class="hljs-selector-tag">i</span>: <span class="hljs-selector-tag">5</span> <span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">i</span><span class="hljs-selector-class">.split</span>(<span class="hljs-string">'|'</span>)<span class="hljs-selector-attr">[2]</span> <span class="hljs-selector-tag">6</span> <span class="hljs-selector-tag">7</span> # 返回车站中文缩写 <span class="hljs-selector-tag">8</span> <span class="hljs-selector-tag">def</span> <span class="hljs-selector-tag">get_city</span>(self, station): <span class="hljs-selector-tag">9</span> <span class="hljs-selector-tag">for</span> <span class="hljs-selector-tag">i</span> <span class="hljs-selector-tag">in</span> <span class="hljs-selector-tag">self</span><span class="hljs-selector-class">.station_data</span><span class="hljs-selector-pseudo">:</span> <span class="hljs-selector-pseudo">10</span> <span class="hljs-selector-tag">if</span> <span class="hljs-selector-tag">station</span> <span class="hljs-selector-tag">in</span> <span class="hljs-selector-tag">i</span><span class="hljs-selector-pseudo">:</span> <span class="hljs-selector-pseudo">11</span> <span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">i</span><span class="hljs-selector-class">.split</span>(<span class="hljs-string">'|'</span>)<span class="hljs-selector-attr">[1]</span> |
由于ajax接口有了一点变化,所以我对之前的代码做了一点修改,在输入数据的部分:
1 2 3 4 5 6 7 |
<span class="hljs-number">1</span> # 需要按<span class="hljs-number">2018</span><span class="hljs-number">-01</span><span class="hljs-number">-01</span>的格式输入日期,不然会出现错误 <span class="hljs-number">2</span> d = <span class="hljs-built_in">input</span>(<span class="hljs-string">"请输入日期(如:2018-01-01):"</span>) <span class="hljs-number">3</span> f = self.get_station(<span class="hljs-built_in">input</span>(<span class="hljs-string">"请输入您的出发站:"</span>)) <span class="hljs-number">4</span> t = self.get_station(<span class="hljs-built_in">input</span>(<span class="hljs-string">"请输入您的目的站:"</span>)) <span class="hljs-number">5</span> url = <span class="hljs-string">"https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date={}&leftTicketDTO.from_station={}"</span> \ <span class="hljs-number">6</span> <span class="hljs-string">"&leftTicketDTO.to_station={}&purpose_codes=ADULT"</span>.<span class="hljs-built_in">format</span>(d, f, t) |
完整代码
CJYDemo.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<span class="hljs-keyword">import</span> requests from hashlib <span class="hljs-keyword">import</span> md5 class CJYClient(object): <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, username, password, soft_id)</span>:</span> self.username = username self.password = md5(password.encode(<span class="hljs-string">'utf8'</span>)).hexdigest() self.soft_id = soft_id self.base_params = { <span class="hljs-string">'user'</span>: self.username, <span class="hljs-string">'pass2'</span>: self.password, <span class="hljs-string">'softid'</span>: self.soft_id, } self.headers = { <span class="hljs-string">'Connection'</span>: <span class="hljs-string">'Keep-Alive'</span>, <span class="hljs-string">'User-Agent'</span>: <span class="hljs-string">'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)'</span>, } <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">PostPic</span><span class="hljs-params">(self, im, codetype)</span>:</span> <span class="hljs-string">"""</span> <span class="hljs-string"> im: 图片字节</span> <span class="hljs-string"> codetype: 题目类型 参考 http://www.chaojiying.com/price.html</span> <span class="hljs-string"> """</span> params = { <span class="hljs-string">'codetype'</span>: codetype, } params.update(self.base_params) files = {<span class="hljs-string">'userfile'</span>: (<span class="hljs-string">'ccc.jpg'</span>, im)} r = requests.post(<span class="hljs-string">'http://upload.chaojiying.net/Upload/Processing.php'</span>, data=params, files=files, headers=self.headers) <span class="hljs-keyword">return</span> r.json() <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">ReportError</span><span class="hljs-params">(self, im_id)</span>:</span> <span class="hljs-string">"""</span> <span class="hljs-string"> im_id:报错题目的图片ID</span> <span class="hljs-string"> """</span> params = { <span class="hljs-string">'id'</span>: im_id, } params.update(self.base_params) r = requests.post(<span class="hljs-string">'http://upload.chaojiying.net/Upload/ReportError.php'</span>, data=params, headers=self.headers) <span class="hljs-keyword">return</span> r.json() <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">use_cjy</span><span class="hljs-params">(filename)</span>:</span> username = <span class="hljs-string">""</span> <span class="hljs-comment"># 用户名</span> <span class="hljs-comment"> password = "" # 密码</span> <span class="hljs-comment"> app_id = "" # 软件ID</span> <span class="hljs-comment"> cjy = CJYClient(username, password, app_id) # 用户中心>>软件ID</span> <span class="hljs-comment"> im = open(filename, 'rb').read() # 本地图片文件路径</span> <span class="hljs-comment"> return cjy.PostPic(im, 9004) # 9004->验证码类型</span> |
test.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import jsonimport requestsfrom PIL import Imagefrom time import sleepfrom .CJYDemo import use_cjyfrom selenium import webdriverfrom selenium.webdriver import ActionChainsfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECheaders = { <span class="hljs-string">"User-Agent"</span>: <span class="hljs-string">"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1"</span> <span class="hljs-string">" Safari/537.1"</span> }<span class="hljs-keyword">class</span> TrainUser: def __init__(<span class="hljs-keyword">self</span>, username, password): <span class="hljs-keyword">self</span>.username = username <span class="hljs-keyword">self</span>.password = password <span class="hljs-keyword">self</span>.cookie = <span class="hljs-string">""</span> <span class="hljs-keyword">self</span>.station_data = <span class="hljs-string">""</span> # 登录<span class="hljs-number">12306</span> def login(<span class="hljs-keyword">self</span>): browser = webdriver.Chrome() browser.get(<span class="hljs-string">"https://kyfw.12306.cn/otn/login/init"</span>) browser.find_element_by_xpath(<span class="hljs-string">'//*[@id="username"]'</span>).send_keys(<span class="hljs-keyword">self</span>.username) sleep(<span class="hljs-number">2</span>) browser.find_element_by_xpath(<span class="hljs-string">'//*[@id="password"]'</span>).send_keys(<span class="hljs-keyword">self</span>.password) sleep(<span class="hljs-number">2</span>) captcha_img = browser.find_element_by_xpath(<span class="hljs-string">'//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img'</span>) location = captcha_img.location size = captcha_img.size # 写成我们需要截取的位置坐标 coordinates = (<span class="hljs-keyword">int</span>(location[<span class="hljs-string">'x'</span>]), <span class="hljs-keyword">int</span>(location[<span class="hljs-string">'y'</span>]), <span class="hljs-keyword">int</span>(location[<span class="hljs-string">'x'</span>] + <span class="hljs-number">2</span> * size[<span class="hljs-string">'width'</span>]), <span class="hljs-keyword">int</span>(location[<span class="hljs-string">'y'</span>] + <span class="hljs-number">2</span> * size[<span class="hljs-string">'height'</span>])) browser.save_screenshot(<span class="hljs-string">'screen.png'</span>) i = Image.open(<span class="hljs-string">'screen.png'</span>) # 使用Image的crop函数,从截图中再次截取我们需要的区域 verify_code_image = i.crop(coordinates) verify_code_image.save(<span class="hljs-string">'captcha.png'</span>) # 调用超级鹰识别验证码 capture_result = use_cjy(<span class="hljs-string">'captcha.png'</span>) print(capture_result) # 对返回的结果进行解析 groups = capture_result.get(<span class="hljs-string">"pic_str"</span>).split(<span class="hljs-string">'|'</span>) points = [[<span class="hljs-keyword">int</span>(number) <span class="hljs-keyword">for</span> number <span class="hljs-keyword">in</span> group.split(<span class="hljs-string">','</span>)] <span class="hljs-keyword">for</span> group <span class="hljs-keyword">in</span> groups] <span class="hljs-keyword">for</span> point <span class="hljs-keyword">in</span> points: # 先定位到验证图片 element = WebDriverWait(browser, <span class="hljs-number">20</span>).until( EC.presence_of_element_located((By.CLASS_NAME, <span class="hljs-string">"touclick-image"</span>))) # 模拟点击验证图片 ActionChains(browser).move_to_element_with_offset(element, point[<span class="hljs-number">0</span>] - <span class="hljs-number">110</span>, point[<span class="hljs-number">1</span>] - <span class="hljs-number">90</span>).click().perform() sleep(<span class="hljs-number">3</span>) browser.find_element_by_xpath(<span class="hljs-string">'//*[@id="loginSub"]'</span>).click() sleep(<span class="hljs-number">5</span>) <span class="hljs-keyword">if</span> browser.current_url not <span class="hljs-keyword">in</span> [<span class="hljs-string">"https://kyfw.12306.cn/otn/login/init"</span>, <span class="hljs-string">"https://kyfw.12306.cn/otn/login/init#"</span>]: print(<span class="hljs-string">"登录成功!"</span>) <span class="hljs-keyword">else</span>: print(<span class="hljs-string">"登录失败,请重试!"</span>) # 显示可购车票信息 def show_ticket(<span class="hljs-keyword">self</span>): # 请求保存列车站点代码的链接 res1 = requests.get(<span class="hljs-string">"https://kyfw.12306.cn/otn/resources/js/framework/station_name.js"</span>) # 把分割处理后的车站信息保存在station_data中 <span class="hljs-keyword">self</span>.station_data = res1.text.lstrip(<span class="hljs-string">"var station_names ='"</span>).rstrip(<span class="hljs-string">"'"</span>).split(<span class="hljs-string">'@'</span>) # 需要按<span class="hljs-number">2018</span><span class="hljs-number">-01</span><span class="hljs-number">-01</span>的格式输入日期,不然会出现错误 d = input(<span class="hljs-string">"请输入日期(如:2018-01-01):"</span>) f = <span class="hljs-keyword">self</span>.get_station(input(<span class="hljs-string">"请输入您的出发站:"</span>)) t = <span class="hljs-keyword">self</span>.get_station(input(<span class="hljs-string">"请输入您的目的站:"</span>)) url = <span class="hljs-string">"https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date={}&leftTicketDTO.from_station={}"</span> \ <span class="hljs-string">"&leftTicketDTO.to_station={}&purpose_codes=ADULT"</span>.format(d, f, t) res2 = requests.get(url) result = json.loads(res2.text)[<span class="hljs-string">'data'</span>][<span class="hljs-string">'result'</span>] seat_data = [(<span class="hljs-number">32</span>, <span class="hljs-string">"商务座"</span>), (<span class="hljs-number">31</span>, <span class="hljs-string">"一等座"</span>), (<span class="hljs-number">30</span>, <span class="hljs-string">"二等座"</span>), (<span class="hljs-number">26</span>, <span class="hljs-string">"无座"</span>), (<span class="hljs-number">23</span>, <span class="hljs-string">"软卧"</span>), (<span class="hljs-number">28</span>, <span class="hljs-string">"硬卧"</span>), (<span class="hljs-number">29</span>, <span class="hljs-string">"硬座"</span>)] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> result: i = i.split(<span class="hljs-string">'|'</span>) info = { <span class="hljs-string">"车次"</span>: i[<span class="hljs-number">3</span>], <span class="hljs-string">"出发日期"</span>: i[<span class="hljs-number">13</span>], <span class="hljs-string">"始发站"</span>: <span class="hljs-keyword">self</span>.get_city(i[<span class="hljs-number">4</span>]), <span class="hljs-string">"终点站"</span>: <span class="hljs-keyword">self</span>.get_city(i[<span class="hljs-number">7</span>]), <span class="hljs-string">"出发站"</span>: <span class="hljs-keyword">self</span>.get_city(i[<span class="hljs-number">6</span>]), <span class="hljs-string">"目的站"</span>: <span class="hljs-keyword">self</span>.get_city(i[<span class="hljs-number">5</span>]), <span class="hljs-string">"出发时间"</span>: i[<span class="hljs-number">8</span>], <span class="hljs-string">"到达时间"</span>: i[<span class="hljs-number">9</span>], <span class="hljs-string">"总耗时"</span>: str(<span class="hljs-keyword">int</span>(i[<span class="hljs-number">10</span>][:i[<span class="hljs-number">10</span>].index(<span class="hljs-string">":"</span>)])) + <span class="hljs-string">"小时"</span> + str(<span class="hljs-keyword">int</span>(i[<span class="hljs-number">10</span>][i[<span class="hljs-number">10</span>].index(<span class="hljs-string">":"</span>) + <span class="hljs-number">1</span>:])) + <span class="hljs-string">"分钟"</span>, <span class="hljs-string">"商务座"</span>: <span class="hljs-string">''</span>, <span class="hljs-string">"一等座"</span>: <span class="hljs-string">''</span>, <span class="hljs-string">"二等座"</span>: <span class="hljs-string">''</span>, <span class="hljs-string">"无座"</span>: <span class="hljs-string">''</span>, <span class="hljs-string">"软卧"</span>: <span class="hljs-string">''</span>, <span class="hljs-string">"硬卧"</span>: <span class="hljs-string">''</span>, <span class="hljs-string">"硬座"</span>: <span class="hljs-string">''</span> } <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(<span class="hljs-number">7</span>): <span class="hljs-keyword">if</span> i[seat_data[j][<span class="hljs-number">0</span>]] == <span class="hljs-string">"有"</span> or i[seat_data[j][<span class="hljs-number">0</span>]].isdigit(): info[seat_data[j][<span class="hljs-number">1</span>]] = i[seat_data[j][<span class="hljs-number">0</span>]] <span class="hljs-keyword">else</span>: del info[seat_data[j][<span class="hljs-number">1</span>]] print(info) # 返回车站英文缩写 def get_station(<span class="hljs-keyword">self</span>, city): <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-keyword">self</span>.station_data: <span class="hljs-keyword">if</span> city <span class="hljs-keyword">in</span> i: <span class="hljs-keyword">return</span> i.split(<span class="hljs-string">'|'</span>)[<span class="hljs-number">2</span>] # 返回车站中文缩写 def get_city(<span class="hljs-keyword">self</span>, station): <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-keyword">self</span>.station_data: <span class="hljs-keyword">if</span> station <span class="hljs-keyword">in</span> i: <span class="hljs-keyword">return</span> i.split(<span class="hljs-string">'|'</span>)[<span class="hljs-number">1</span>] <span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>: u = TrainUser(input(<span class="hljs-string">"请输入您的用户名:"</span>), input(<span class="hljs-string">"请输入您的密码:"</span>)) u.login() u.show_ticket() |
https://m.toutiaocdn.com/i6847380490597433867/?app=news_article×tamp=1594333456&use_new_style=1&req_id=20200710062416010014048145012F04E7&group_id=6847380490597433867&tt_from=android_share&utm_medium=toutiao_android&utm_campaign=client_share
转载请注明:徐自远的乱七八糟小站 » python实现登录12306网站查看火车票信息