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()
相关推荐
行思理几秒前
MongoDB 大数据备份,新手教程
数据库·mongodb
江华森25 分钟前
Python 进阶编程实战 — 从多版本环境到百万级登录系统
python
C+-C资深大佬27 分钟前
python while循环
服务器·开发语言·python
城数派1 小时前
1950-2026年中国0.1°逐月平均气温栅格数据集
数据库·信息可视化
livemetee1 小时前
【关于redis高性能,高可用处理】
数据库·redis·缓存
-To be number.wan1 小时前
数据库系统 | 数据库安全与完整性
数据库·学习
zh路西法2 小时前
【现代控制理论与卡尔曼滤波】从状态空间到Python仿真实现
开发语言·python
Vodka~2 小时前
WSL2 + RViz GPU渲染机械臂
人工智能·python
8Qi82 小时前
hello-agents学习笔记--Memory让Agent拥有记忆
人工智能·python·llm·agent·ai编程·vibecoding
Omics Pro2 小时前
首个针对生物医药LLM智能体的全流程过程级评测框架
数据库·人工智能·windows·redis·量子计算