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()
相关推荐
yangmf2040几秒前
INFINI Gateway 助力联想集团 ES 迁移升级
大数据·数据库·elasticsearch·搜索引擎·gateway·全文检索
码农阿豪6 分钟前
MySQL 亿级大表(1.35亿条)安全添加字段实战指南
数据库·mysql·安全
我可以将你更新哟11 分钟前
【爬虫】下载ffmpeg,爬取b站视频,把音频和视频合成一个视频
爬虫·ffmpeg·音视频
典龙33012 分钟前
推荐一款开源免费的AI智能数据库工具,支持连接mysql,oracle,postgresql,ssh,redis
数据库·mysql
怎么没有名字注册了啊12 分钟前
VMware 完整版安装 Debian 纯命令行系统(无图形化、超详细全程教程)
linux·服务器·网络·数据库·debian
此生只爱蛋19 分钟前
【Redis】Zset 有序集合
数据库·redis·缓存
睿思达DBA_WGX20 分钟前
Oracle 服务器 ORA-12516 错误的处理过程
服务器·数据库·oracle
胡伯来了20 分钟前
08 - 数据收集 - 网页采集工具Selenium
爬虫·python·selenium·rag·网络采集
程序员小白条21 分钟前
提前实习的好处有哪些?有坏处吗?
java·开发语言·数据结构·数据库·链表
星辰_mya21 分钟前
redis数据丢失之脑裂
数据库·redis·缓存