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 相关文章推荐
wxPython窗口的继承机制实例分析
Sep 28 Python
Python实现的下载8000首儿歌的代码分享
Nov 21 Python
打包发布Python模块的方法详解
Sep 18 Python
解决Django模板无法使用perms变量问题的方法
Sep 10 Python
python快速建立超简单的web服务器的实现方法
Feb 17 Python
Python数据结构之哈夫曼树定义与使用方法示例
Apr 22 Python
python 每天如何定时启动爬虫任务(实现方法分享)
May 21 Python
Python决策树之基于信息增益的特征选择示例
Jun 25 Python
python getpass模块用法及实例详解
Oct 07 Python
python中数据库like模糊查询方式
Mar 02 Python
python如何安装下载后的模块
Jul 03 Python
python如何导出微信公众号文章方法详解
Aug 31 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怎么实现网站保存快捷方式方便用户随时浏览
2013/08/15 PHP
php简单获取目录列表的方法
2015/03/24 PHP
js post方式传递提交的实现代码
2010/05/31 Javascript
js ondocumentready onmouseover onclick onmouseout 样式
2010/07/22 Javascript
js中将具有数字属性名的对象转换为数组
2011/03/06 Javascript
Js 去掉字符串中的空格(实现代码)
2013/11/19 Javascript
浅析jQuery Ajax请求参数和返回数据的处理
2016/02/24 Javascript
js实现的简练高效拖拽功能示例
2016/12/21 Javascript
微信小程序  checkbox组件详解及简单实例
2017/01/10 Javascript
利用Javascript裁剪图片并存储的简单实现
2017/03/13 Javascript
MUI 实现侧滑菜单及其主体部分上下滑动的方法
2018/01/25 Javascript
mpvue开发音频类小程序踩坑和建议详解
2019/03/12 Javascript
webpack4实现不同的导出类型
2019/04/09 Javascript
vue 兄弟组件的信息传递的方法实例详解
2019/08/30 Javascript
Python利用IPython提高开发效率
2016/08/10 Python
Python 记录日志的灵活性和可配置性介绍
2018/02/27 Python
详解windows python3.7安装numpy问题的解决方法
2018/08/13 Python
centos6.5安装python3.7.1之后无法使用pip的解决方案
2019/02/14 Python
Python3安装Pillow与PIL的方法
2019/04/03 Python
python pytest进阶之conftest.py详解
2019/06/27 Python
python如何爬取网站数据并进行数据可视化
2019/07/08 Python
Python在字符串中处理html和xml的方法
2020/07/31 Python
scrapy头部修改的方法详解
2020/12/06 Python
python help函数实例用法
2020/12/06 Python
遮罩层 + Iframe实现界面自动显示的示例代码
2020/04/26 HTML / CSS
英国50岁以上人群的交友网站:Ourtime
2018/03/28 全球购物
马来西亚网上购物:Youbeli
2018/03/30 全球购物
日本索尼音乐商店:Sony Music Shop
2018/07/17 全球购物
乌克兰设计师和品牌的服装:Love&Live
2020/04/14 全球购物
我的画教学反思
2014/04/28 职场文书
优秀应届毕业生自荐书
2014/06/29 职场文书
教师学习三严三实心得体会
2014/10/13 职场文书
民用住房租房协议书
2014/10/29 职场文书
2016学习雷锋精神活动倡议书
2015/04/27 职场文书
环保证明
2015/06/23 职场文书
MYSQL数据库使用UTF-8中文编码乱码的解决办法
2021/05/26 MySQL