Python脚本抢票【笔记】
- 前言
- 版权
- 推荐
- Python脚本抢票【Python】
- microsoft edge驱动器下载以及使用
- 最后
前言
2024-4-17 18:19:15
以下内容源自《【笔记】》
仅供学习交流使用
版权
禁止其他平台发布时删除以下此话
本文首次发布于CSDN平台
作者是CSDN@日星月云
博客主页是https://jsss-1.blog.csdn.net
禁止其他平台发布时删除以上此话
推荐
打响春运第一枪,为购票做准备,用Python制作一个12306抢票脚本!
python最新抢票脚本
Python脚本抢票【Python】
试了一下,没有实现。
import time
import datetime
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class Qiangpiao():def __init__(self,from_station,to_station,depart_time,train_num,passenger):self.login_url = 'https://kyfw.12306.cn/otn/resources/login.html'self.init_my_url = 'https://kyfw.12306.cn/otn/view/index.html'self.order_url = 'https://kyfw.12306.cn/otn/confirmPassenger/initDc'# input("出发地:")self.from_station = from_station# input("目的地:")self.to_station = to_station# 时间格式必须是M-d的方式# input("出发时间(格式必须是M-d的方式):")self.depart_time = depart_time# input("列车号:")self.train_num = train_numself.passenger = passenger#获取当前月份self.now_month = datetime.date.today().monthself.leave_month = int(self.depart_time.split('-')[0])self.leave_day = int(self.depart_time.split('-')[1])self.driver = webdriver.Chrome()def _login(self):self.driver.get(self.login_url)# 窗口最大化#self.driver.maximize_window()# 设置窗口大小self.driver.set_window_size(1300,800)#print('调整前尺寸:', self.driver.get_window_size())#显式等待#这里进行手动登录,可以扫码,也可以输入账号密码点击登录WebDriverWait(self.driver,1000).until(EC.url_to_be(self.init_my_url))print('登录成功!')def _pop_window(self):time.sleep(1)self.driver.find_element(By.XPATH,'//*[@class="dzp-confirm"]/div[2]/div[3]/a').click()def _enter_order_ticket(self):action = ActionChains(self.driver) # 实例化一个动作链对象element = self.driver.find_element("link text", '车票')# 鼠标移动到 '车票' 元素上的中心点action.move_to_element(element).perform()# 点击'单程'self.driver.find_element(By.XPATH,'//*[@id="J-chepiao"]/div/div[1]/ul/li[1]/a').click()# 消除第二次弹窗# self.driver.find_element("link text",'确认').click()def _search_ticket(self):#出发地输入self.driver.find_element(By.ID,"fromStationText").click()self.driver.find_element(By.ID,"fromStationText").send_keys(self.from_station)# 需要检索信息self.driver.find_element(By.ID,"panel_cities").find_element(By.CLASS_NAME,self.from_station).send_keys(Keys.ENTER)# self.driver.find_element(By.ID,"fromStationText").send_keys(Keys.ENTER)# city_elements=self.driver.find_element(By.ID,"panel_cities").find_element(By.XPATH, "//span[contains(text(), '西安')]")# # 循环遍历找到的元素,点击符合条件的元素# for element in city_elements:# if element.text == self.from_station:# element.click()#目的地输入self.driver.find_element(By.ID,"toStationText").click()self.driver.find_element(By.ID,"toStationText").send_keys(self.to_station)self.driver.find_element(By.ID,"toStationText").send_keys(Keys.ENTER)#出发日期输入self.driver.find_element(By.ID,"train_date").click()if self.leave_month == self.now_month:xpath_str = f"//div[@class='cal-wrap']/div[1]/div[2]/div[{self.leave_day}]"if EC.element_to_be_clickable((By.XPATH, xpath_str)):self.driver.find_element(By.XPATH,xpath_str).click()else:print("当前日期未到或已过售票日期,无法购票!")elif self.leave_month == self.now_month + 1:xpath_str = f"//div[@class='cal-wrap']/div[2]/div[2]/div[{self.leave_day}]"if EC.element_to_be_clickable((By.XPATH, xpath_str)):self.driver.find_element(By.XPATH,xpath_str).click()else:print("当前日期未到或已过售票日期,无法购票!")else:print("月份超前一个月以上,无法购票!")#等待查询按钮是否可用WebDriverWait(self.driver,1000).until(EC.element_to_be_clickable((By.ID,"query_ticket")))#执行点击事件search_btn = self.driver.find_element(By.ID,"query_ticket")search_btn.click()#等待查票信息加载WebDriverWait(self.driver, 1000).until(EC.presence_of_element_located((By.XPATH, '//*[@id="queryLeftTable"]/tr')))def _order_ticket(self):train_num_list = [] # 列车号列表train_num_ele_list = self.driver.find_elements(By.XPATH,'//tr/td[1]/div/div[1]/div/a') # 列车号元素列表for t in train_num_ele_list: # 遍历列车号元素列表,并把列车号添加到列车号列表train_num_list.append(t.text)tr_list = self.driver.find_elements(By.XPATH,'//*[@id="queryLeftTable"]/tr[not(@datatran)]') #每一列列车整行信息列表,列车号元素是tr的子元素if self.train_num in train_num_list:for tr in tr_list:train_num = tr.find_element(By.XPATH,"./td[1]/div/div[1]/div/a").text #取出元素tr里的列车号if self.train_num == train_num:#动车二等座余票信息text_1 = tr.find_element(By.XPATH,"./td[4]").text# 火车二等座余票信息text_2 = tr.find_element(By.XPATH,"./td[8]").textif (text_1 == "有" or text_1.isdigit()) or (text_2 == "有" or text_2.isdigit()):#点击预订按钮order_btn = tr.find_element(By.CLASS_NAME,"btn72")order_btn.click()#等待订票页面WebDriverWait(self.driver,1000).until(EC.url_to_be(self.order_url))# 选定乘车人self.driver.find_element(By.XPATH,f'//*[@id="normal_passenger_id"]/li/label[contains(text(),"{self.passenger}")]').click()#如果乘客是学生,对提示点击确定if EC.presence_of_element_located((By.XPATH, '//div[@id="dialog_xsertcj"]')):self.driver.find_element(By.ID,'dialog_xsertcj_ok').click()# 提交订单self.driver.find_element(By.ID,'submitOrder_id').click()time.sleep(2)# 点击确认订单self.driver.find_element(By.ID,'qr_submit_id').click()else:# 提交订单self.driver.find_element(By.ID,'submitOrder_id').click()time.sleep(2)# 点击确认self.driver.find_element(By.ID,'qr_submit_id').click()print("购票成功!")breakelse:print("二等座无票!")else:print("无此列车!")def run(self):#登录self._login()#消除登录后(第一次)的弹窗# self._pop_window()#进入购票页面self._enter_order_ticket()#查票self._search_ticket()#订票self._order_ticket()#关闭浏览器time.sleep(6)self.driver.quit()if __name__ == '__main__':qiangpiao = Qiangpiao("西安","绥德","4-30","K214","姬烁")qiangpiao.run()'''
[python最新抢票脚本]
(https://blog.csdn.net/qq_57421630/article/details/119152956)
'''
microsoft edge驱动器下载以及使用
microsoft edge驱动器下载以及使用
最后
2024-4-27 15:50:10
迎着日光月光星光,直面风霜雨霜雪霜。