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按行读取文件的简单实现方法
Jun 22 Python
Python对文件和目录进行操作的方法(file对象/os/os.path/shutil 模块)
May 08 Python
python字符串过滤性能比较5种方法
Jun 22 Python
python递归函数绘制分形树的方法
Jun 22 Python
python实现可变变量名方法详解
Jul 01 Python
nginx黑名单和django限速,最简单的防恶意请求方法分享
Aug 09 Python
python绘制BA无标度网络示例代码
Nov 21 Python
解决pyPdf和pyPdf2在合并pdf时出现异常的问题
Apr 03 Python
Python多线程实现支付模拟请求过程解析
Apr 21 Python
部署Django到阿里云服务器教程示例
Jun 03 Python
Python爬虫HTPP请求方法有哪些
Jun 03 Python
python 实现socket服务端并发的四种方式
Dec 14 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
PHP中的reflection反射机制测试例子
2014/08/05 PHP
PHP+Ajax无刷新带进度条图片上传示例
2017/02/08 PHP
php实现的双色球算法示例
2017/06/20 PHP
Laravel中错误与异常处理的用法示例
2018/09/16 PHP
php微信公众号开发之二级菜单
2018/10/20 PHP
实例说明js脚本语言和php脚本语言的区别
2019/04/04 PHP
JS中Iframe之间传值的方法
2013/03/11 Javascript
jQuery .tmpl() 用法示例介绍
2014/08/21 Javascript
浅谈js的setInterval事件
2014/12/05 Javascript
JS实现至少包含字母、大小写数字、字符的密码等级的两种方法
2015/02/03 Javascript
js实现背景图片感应鼠标变化的方法
2015/02/28 Javascript
js实现简单的左右两边固定广告效果实例
2015/04/10 Javascript
使用AngularJS和PHP的Laravel实现单页评论的方法
2015/06/19 Javascript
省市区三级联动下拉框菜单javascript版
2015/08/11 Javascript
巧用数组制作图片切换js代码
2016/11/29 Javascript
JavaScript字符串_动力节点Java学院整理
2017/06/27 Javascript
详解EasyUi控件中的Datagrid
2017/08/23 Javascript
Angular7中创建组件/自定义指令/管道的方法实例详解
2019/04/02 Javascript
Python群发邮件实例代码
2014/01/03 Python
速记Python布尔值
2017/11/09 Python
Python数据类型中的“冒号“[::]——分片与步长操作示例
2018/01/24 Python
Python wxpython模块响应鼠标拖动事件操作示例
2018/08/23 Python
python+pygame实现坦克大战
2019/09/10 Python
Python 私有化操作实例分析
2019/11/21 Python
Python使用Chrome插件实现爬虫过程图解
2020/06/09 Python
Python图像阈值化处理及算法比对实例解析
2020/06/19 Python
浅谈CSS3中display属性的Flex布局的方法
2017/08/14 HTML / CSS
美国电视购物HSN官网:HSN
2016/09/07 全球购物
澳大利亚Rockwear官网:女子瑜伽、健身和运动服
2021/01/26 全球购物
教育英语专业毕业生的求职信
2014/03/13 职场文书
产品质量承诺范本
2014/03/31 职场文书
父母对孩子说的话
2014/04/12 职场文书
总经理检讨书
2014/09/15 职场文书
2014年卫生院工作总结
2014/12/03 职场文书
千与千寻观后感
2015/06/04 职场文书
幼儿园大班教育随笔
2015/08/14 职场文书