python解决12306登录验证码的实现


Posted in Python onApril 18, 2021

在家无聊,线代和高数看不懂,整点事情干,就准备预定回学校的高铁票,于是就有了这个文章

准备工作

1.pip安装chromediver,当然也可以手动解压(网上的教程好像没有提到pip,手动安装到C盘pycharm里面的Scripts就行了)
chromedriver.storage.googleapis.com/index.html这是chromedriver文件官网,在chrome里面设置查看自己的版本,然后找对应的版本就完了

2.注册个超级鹰,http://www.chaojiying.com/contact.html,挺厉害的打码平台,微信公众号绑定一下账号给1000积分,足够干12306验证码了

开始实战讲解

1.选择chrome打开12306然后切换到账号登录

默认是扫码登录

python解决12306登录验证码的实现

F12然后点击账号登录

python解决12306登录验证码的实现

3.复制xPath,/html/body/div[2]/div[2]/ul/li[2]/a

python解决12306登录验证码的实现

代码实现

from selenium.webdriver import Chrome
web = Chrome()
web.get('https://kyfw.12306.cn/otn/resources/login.html')
time.sleep(3)
web.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click()

2.下载验证码(截屏也可以)然后发送给超级鹰

超级鹰官网有个官方文档,下载然后pychram打开,其实就很简单,然后把账号密码改成你自己的,

from chaojiying import Chaojiying_Client

验证码需要时间加载,所以要sleep(3)就够了,

3.拿到坐标然后模拟点击
好像这个官方叫什么偏移量,挺高大上的,说白了就是建立一个坐标系,给个x,y然后点击就完了,默认左上方是原点

for pre_location in location_list:
        #切割出来x,y坐标
        location = pre_location.split(',')
        x = int(location[0])
        y = int(location[1])
        ActionChains(web).move_to_element_with_offset(img,x,y).click().perform()

4.登录以后有个滑动验证
现在我还没有找到方法控制滑动速度,匀速运动,但是12306并没有因为这个验证失败

ActionChains(web).drag_and_drop_by_offset(button,340,0).perform()

button是那个滑块的Xpath,我记得好像是长度330,340肯定是够用了,那个0就是竖y的方向上的滑动

12306靠webdriver判断是不是爬虫

刚开始12306图片和滑动验证通过以后一直说验证失败,百思不得其解,百度发现是因为这个

python解决12306登录验证码的实现

这是正常页面下的,也就是我改了以后的,加一个这个代码,欺骗一下

def trick_not_chromedriver():
    option = Options()
    option.add_argument('--disable-blink-features=AutomationControlled')
    return option

这个要调用在前面,靠后一点就不行了

全部代码

from selenium.webdriver import Chrome
import requests,time
from hashlib import md5
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
class Chaojiying_Client(object):

    def __init__(self, username, password, soft_id):
        self.username = username
        password =  password.encode('utf8')
        self.password = md5(password).hexdigest()
        self.soft_id = soft_id
        self.base_params = {
            'user': self.username,
            'pass2': self.password,
            'softid': self.soft_id,
        }
        self.headers = {
            'Connection': 'Keep-Alive',
            'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
        }

    def PostPic(self, im, codetype):
        """
        im: 图片字节
        codetype: 题目类型 参考 http://www.chaojiying.com/price.html
        """
        params = {
            'codetype': codetype,
        }
        params.update(self.base_params)
        files = {'userfile': ('ccc.jpg', im)}
        r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
        return r.json()

    def ReportError(self, im_id):
        """
        im_id:报错题目的图片ID
        """
        params = {
            'id': im_id,
        }
        params.update(self.base_params)
        r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
        return r.json()
#获取验证码
def get_verify_img(web):
    web.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click()
    time.sleep(5)
    verify_img = web.find_element_by_xpath('//*[@id="J-loginImg"]')
    return verify_img
#识别验证码返回坐标
def discern_verify_img(verify_img):
    chaojiying = Chaojiying_Client('超级鹰账号', '密码', '软件ID')
    responce = chaojiying.PostPic(verify_img.screenshot_as_png, 9004)
    pre_location = responce['pic_str']
    location_list = pre_location.split("|")
    # 把split写错了,卡了半天
    # type_pre_location = type(pre_location)
    return location_list
    # return type_pre_location
#拿到坐标模拟点击
def click_and_enter(web,location_list,img):
    for pre_location in location_list:
        #切割出来x,y坐标
        location = pre_location.split(',')
        x = int(location[0])
        y = int(location[1])
        ActionChains(web).move_to_element_with_offset(img,x,y).click().perform()
def enter(web):
    # input()
    web.find_element_by_xpath('//*[@id="J-userName"]').send_keys('账号')
    web.find_element_by_xpath('//*[@id="J-password"]').send_keys('密码')
    web.find_element_by_xpath('//*[@id="J-login"]').click()
#滑动验证
def move_verify(web):
    button = web.find_element_by_xpath('//*[@id="nc_1__scale_text"]/span')
    ActionChains(web).drag_and_drop_by_offset(button,340,0).perform()
# 骗12306这不是chromedriver
def trick_not_chromedriver():
    option = Options()
    option.add_argument('--disable-blink-features=AutomationControlled')
    return option
#现在有一个疫情防控的确认按钮,点一下这个
def yqfk(web):
    web.get('https://kyfw.12306.cn/otn/leftTicket/init')
    time.sleep(1)
    web.find_element_by_xpath('//*[@id="qd_closeDefaultWarningWindowDialog_id"]').click()
#进入查询界面,思路正则表达式,不可信
def get_stick_text(web):
    web.get('https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2021-04-16&leftTicketDTO.from_station=TNV&leftTicketDTO.to_station=CZF&purpose_codes=0X00')
    response = web.find_element_by_xpath('/html/body/pre').text
    return (response)
#父子节点一个一个找,显示余票
if __name__ == '__main__':
    web = Chrome(options=trick_not_chromedriver())
    web.get('https://kyfw.12306.cn/otn/resources/login.html')
    time.sleep(5)
    # click_and_enter(discern_verify_img(get_verify_img()))
    img = get_verify_img(web)
    click_and_enter(web,discern_verify_img(img),img)
    time.sleep(5)
    enter(web)
    time.sleep(5)
    move_verify(web)
    time.sleep(1)
    yqfk(web)
    time.sleep(2)
    get_verify_img(web)

已经可以登录的,结果就是这个界面

python解决12306登录验证码的实现

还有一个想法是余票检测,在搞了,应该快了

到此这篇关于python解决12306登录验证码的实现的文章就介绍到这了,更多相关python 12306登录验证码 内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python群发邮件实例代码
Jan 03 Python
Python之eval()函数危险性浅析
Jul 03 Python
用于统计项目中代码总行数的Python脚本分享
Apr 21 Python
python查找指定具有相同内容文件的方法
Jun 28 Python
python利用正则表达式排除集合中字符的功能示例
Oct 10 Python
Tensorflow使用支持向量机拟合线性回归
Sep 07 Python
Python3使用Matplotlib 绘制精美的数学函数图形
Apr 11 Python
Python figure参数及subplot子图绘制代码
Apr 18 Python
查看keras各种网络结构各层的名字方式
Jun 11 Python
解决Alexnet训练模型在每个epoch中准确率和loss都会一升一降问题
Jun 17 Python
安装不同版本的tensorflow与models方法实现
Feb 20 Python
4种非常实用的python内置数据结构
Apr 28 Python
django注册用邮箱发送验证码的实现
Apr 18 #Python
Python控制台输出俄罗斯方块的方法实例
Apr 17 #Python
python3 实现mysql数据库连接池的示例代码
Python如何利用正则表达式爬取网页信息及图片
Apr 17 #Python
python中sys模块的介绍与实例
Apr 17 #Python
Python中os模块的简单使用及重命名操作
Apr 17 #Python
Python利器openpyxl之操作excel表格
You might like
删除html标签得到纯文本可处理嵌套的标签
2014/04/28 PHP
php保存任意网络图片到服务器的方法
2015/04/14 PHP
PHP如何使用Memcached
2016/04/05 PHP
Lumen timezone 时区设置方法(慢了8个小时)
2018/01/20 PHP
比较全的JS checkbox全选、取消全选、删除功能代码
2008/12/19 Javascript
网易JS面试题与Javascript词法作用域说明
2010/11/09 Javascript
22点关于jquery性能优化的建议
2014/05/28 Javascript
原生js事件的添加和删除的封装
2014/07/01 Javascript
JavaScript 作用域链解析
2014/11/13 Javascript
jquery解决客户端跨域访问问题
2015/01/06 Javascript
Javascript定义类(class)的三种方法详解
2015/03/13 Javascript
js实现简单折叠、展开菜单的方法
2015/08/28 Javascript
Layui实现主窗口和Iframe层参数传递
2019/11/14 Javascript
vue学习之Vue-Router用法实例分析
2020/01/06 Javascript
vue 封装 Adminlte3组件的实现
2020/03/18 Javascript
浅谈JavaScript 声明提升
2020/09/14 Javascript
python实现的简单文本类游戏实例
2015/04/28 Python
pandas全表查询定位某个值所在行列的方法
2018/04/12 Python
Python3中的json模块使用详解
2018/05/05 Python
Python实现查看系统启动项功能示例
2018/05/10 Python
Python检测网络延迟的代码
2018/05/15 Python
python实现pdf转换成word/txt纯文本文件
2018/06/07 Python
python远程连接MySQL数据库
2019/04/19 Python
Pandas DataFrame数据的更改、插入新增的列和行的方法
2019/06/25 Python
简单了解python gevent 协程使用及作用
2019/07/22 Python
Django框架 querySet功能解析
2019/09/04 Python
Python多线程正确用法实例解析
2020/05/30 Python
体育纪念品、亲笔签名的体育收藏品:Steiner Sports
2020/07/31 全球购物
运动会广播稿80字
2014/01/23 职场文书
宝宝满月酒主持词和仪式流程
2014/03/27 职场文书
养牛场项目建议书
2014/05/13 职场文书
2015年五四青年节活动总结
2015/02/10 职场文书
行政申诉状范文
2015/05/20 职场文书
创业计划书之家教托管
2019/09/25 职场文书
导游词之蓬莱长岛
2019/12/17 职场文书
Python 居然可以在 Excel 中画画你知道吗
2022/02/15 Python