python爬虫之12306模拟登陆

python爬虫之12306模拟登陆

登录流程:

1、登录界面输入账号密码,点击立即登录

2、弹出手机验证界面,输入身份证后4位,点击获取验证码等待验证码后手动输入,点击确定登录

实现代码如下:

复制代码
#需求:登陆12306,输入短信验证码后登陆
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
#导入动作链对应的类
from selenium.webdriver import ActionChains
from selenium.webdriver.edge.options import Options
edge_options = Options()
edge_options.add_experimental_option("detach", True)
edge_options.add_argument("--guest") # 启用guest模式。
# edge_options.add_experimental_option("detach",True)
# 创建一个Edge WebDriver实例
bro = webdriver.Edge(options=edge_options)

bro.get('https://kyfw.12306.cn/otn/resources/login.html')
handle = bro.current_window_handle  # 获取当前窗口句柄

username = bro.find_element(By.ID,'J-userName')
password = bro.find_element(By.ID,'J-password')

username.send_keys('账号')
password.send_keys('密码')

btn = bro.find_element(By.ID,'J-login')
btn.click()

# card = bro.find_element(By.ID,'id_card')
# card.send_keys('6220')

handles = bro.window_handles  # 获取所有窗口句柄
for window_handle in handles:  # 循环比较句柄
    if window_handle != handle:  # 如果窗口句柄与当前窗口句柄不一致,则切换到该窗口
        bro.switch_to.window(window_handle)
sleep(3)
card = bro.find_element(By.ID,'id_card')
card.send_keys('XXXX')

btn_vertify = bro.find_element(By.ID,'verification_code')
btn_vertify.click()

# code = bro.find_element(By.ID,'code')
sleep(60)
# yanzhengma = input()
# sleep()
# code.send_keys('035145')

btn_primary = bro.find_element(By.ID,'sureClick')
btn.click()

sleep(3)
bro.quit()
相关推荐
databook16 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室17 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三18 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户2519162427111 天前
Python之语言特点
python
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
刘立军1 天前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence1 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
数据智能老司机1 天前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构