【使用python+selenium实现12306的自动登录及购票】

一、流程介绍
1. 访问登录页面
  在此页面输入用户名及密码后,点击立即登录。【使用python+selenium实现12306的自动登录及购票】_第1张图片
注意事项:
  一些网站在使用Selenium时会有前端检测,为了避免这种检测,需要对ChromeOptions进行设置,通过调用add_argument方法添加参数–disable-blink-features。
代码实现:

options = webdriver.ChromeOptions()
options.add_argument('--disable-blink-features=AutomationControlled')  # 避免webdriver检测

driver = webdriver.Chrome(options=options)    # Chrome浏览器
driver.set_window_size(1080, 800);
driver.implicitly_wait(10);

# 打开网页
driver.get("https://kyfw.12306.cn/otn/resources/login.html")
user = driver.find_element_by_id("J-userName");
user.click();
user.send_keys("xxxxxxxx");
pswd = driver.find_element_by_id("J-password");
pswd.click();
pswd.send_keys("xxxxxxxx");
butten = driver.find_element_by_id("J-login");
butten.click();
time.sleep(1);

2.选择验证方式
【使用python+selenium实现12306的自动登录及购票】_第2张图片
注意事项:
  这里的拖拽需要通过 ActionChains来实现。ActionChains用来模拟鼠标操作,比如单击、双击、点击鼠标右键、拖拽等等。
代码实现:

while True:
    try:
        span = driver.find_element_by_id("nc_1_n1z")
        actions = ActionChains(driver)  # 行为链实例化
        time.sleep(1)  # 等待2秒钟
        # 经截图测量,滑块需要滑过的距离为300像素
        actions.click_and_hold(span).move_by_offset(300, 0).perform()  # 滑动
        actions.release();
        time.sleep(1);
        a = driver.find_element_by_id("nc_1_refresh1");# 查找刷新按钮,如果没有说明登录成功,执行except跳出循环
        a.click();# 如果刚刚滑动失败,则点击刷新,重新滑动
    except Exception as e:
        print(e);
        break;

3.关闭提示框,点击“车票预订”链接,跳转至车票预订页面**
【使用python+selenium实现12306的自动登录及购票】_第3张图片【使用python+selenium实现12306的自动登录及购票】_第4张图片
代码实现:

sure = driver.find_element_by_class_name("btn-primary");
sure.click();
link_for_ticket = driver.find_element_by_id("link_for_ticket");
link_for_ticket.click();

4.设置出发地、目的地、出发日、车次类型等信息
【使用python+selenium实现12306的自动登录及购票】_第5张图片
代码实现:

driver.find_element_by_id("fromStationText").click();
driver.find_element_by_css_selector(u"[title=长沙]").click();
driver.find_element_by_id("toStationText").click();
driver.find_element_by_css_selector(u"[title=北京]").click();
time.sleep(5);
train_date = driver.find_element_by_id("train_date");
train_date.clear();
tomorrow = (date.today() + timedelta(days= 1)).strftime("%Y-%m-%d")
train_date.send_keys(tomorrow);
driver.find_element_by_css_selector("#_ul_station_train_code > li:nth-child(1) > label").click()

5.点击“预订”,选择乘车人,完成购票
【使用python+selenium实现12306的自动登录及购票】_第6张图片
代码实现:

while True:
    try:
        driver.find_element_by_id("query_ticket").click();
        driver.find_element_by_xpath("/html/body/div[3]/div[7]/div[8]/table/tbody[1]/tr[1]/td[13]").click();
        time.sleep(3);
        driver.find_element_by_id("normalPassenger_0").click();
        driver.find_element_by_id("submitOrder_id").click();
        driver.find_element_by_link_text("确认").click();
    except:
        pass;

二、完整代码

import time
from datetime import date, timedelta
from selenium import webdriver
from selenium.webdriver import ActionChains

options = webdriver.ChromeOptions()
options.add_argument('--disable-blink-features=AutomationControlled')  # 避免webdriver检测

driver = webdriver.Chrome(options=options)    # Chrome浏览器
driver.set_window_size(1080, 800);
driver.implicitly_wait(10);

# 打开网页
driver.get("https://kyfw.12306.cn/otn/resources/login.html")
user = driver.find_element_by_id("J-userName");
user.click();
user.send_keys("xxxxxxx");
pswd = driver.find_element_by_id("J-password");
pswd.click();
pswd.send_keys("xxxxxxx");
butten = driver.find_element_by_id("J-login");
butten.click();
time.sleep(1);

while True:
    try:
        span = driver.find_element_by_id("nc_1_n1z")
        actions = ActionChains(driver)  # 行为链实例化
        time.sleep(1)  # 等待2秒钟
        # 经截图测量,滑块需要滑过的距离为300像素
        actions.click_and_hold(span).move_by_offset(300, 0).perform()  # 滑动
        actions.release();
        time.sleep(1);
        a = driver.find_element_by_id("nc_1_refresh1");# 查找刷新按钮,如果没有说明登录成功,执行except跳出循环
        a.click();# 如果刚刚滑动失败,则点击刷新,重新滑动
    except Exception as e:
        print(e);
        break;

sure = driver.find_element_by_class_name("btn-primary");
sure.click();

link_for_ticket = driver.find_element_by_id("link_for_ticket");
link_for_ticket.click();
driver.find_element_by_id("fromStationText").click();
driver.find_element_by_css_selector(u"[title=长沙]").click();
driver.find_element_by_id("toStationText").click();
driver.find_element_by_css_selector(u"[title=北京]").click();
time.sleep(5);
train_date = driver.find_element_by_id("train_date");
train_date.clear();

tomorrow = (date.today() + timedelta(days= 1)).strftime("%Y-%m-%d")
train_date.send_keys(tomorrow);
driver.find_element_by_css_selector("#_ul_station_train_code > li:nth-child(1) > label").click()

while True:
    try:
        driver.find_element_by_id("query_ticket").click();
        driver.find_element_by_xpath("/html/body/div[3]/div[7]/div[8]/table/tbody[1]/tr[1]/td[13]").click();
        time.sleep(3);
        driver.find_element_by_id("normalPassenger_0").click();
        driver.find_element_by_id("submitOrder_id").click();
        driver.find_element_by_link_text("确认").click();
    except:
        pass;

你可能感兴趣的:(python,selenium,前端)