12306官网登录的验证码破解比较简单,验证码是常规的点触类型验证码,使用超级鹰识别率比较高。

思路:

(1)webdriver打开浏览器;

(2)先对整个屏幕截屏,通过标签定位找到验证码图片,并定位到验证码图片的坐标,然后从先前截屏的图片中截取验证码部分的图片;

(3)通过超级鹰识别验证码上字的坐标;

(4)点击验证码图片上的字;

(5) 输入用户名和密码进行登录;

注意:将以下标红部分的账号等信息换成自己的即可成功

from selenium import webdriver
from selenium.webdriver import ActionChains
from chaojiying import Chaojiying
from PIL import Image
import time # 用户名和密码是自己的12306账号的用户名和密码
USERNAME =
PASSWORD =
class Huochepiao(): def __init__(self):
self.bro = webdriver.Chrome()
self.url = 'https://kyfw.12306.cn/otn/login/init' def open_browser(self):
self.bro.get(self.url)
time.sleep(5) # def __del__(self):
# self.bro.quit() def find_code_img(self):
# 定位到验证码图片对应的img标签
code_img = self.bro.find_element_by_class_name('touclick-img-par')
location = code_img.location
size = code_img.size
return (location,size) def get_code_img(self,location,size):
# rangle对应的就是验证码图片的裁剪区域
rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height']))
self.bro.save_screenshot('aa.png')
i = Image.open('./aa.png')
frame = i.crop(rangle) # 根据指定区域实现裁剪
frame.save('code.png') def get_words_locations(self):
chaojiying = Chaojiying(username, password, softID) # 超级鹰账号、密码和ID更换即可
im = open('./code.png', 'rb').read()
print("超级鹰识别结果:",chaojiying.post_pic(im, 9004))
result = chaojiying.post_pic(im, 9004)['pic_str']
print("result:",result) # "x1,y1|x2,y2" --> [['x1','y1'],['x2','y2']] "x,y" --> [['x','y']]
all_coorodinates = []
if '|' in result:
li = result.split('|')
count = len(li)
for i in range(count):
xy_list = []
x = int(li[i].split(',')[0])
y = int(li[i].split(',')[1])
xy_list.append(x)
xy_list.append(y)
all_coorodinates.append(xy_list)
else:
x = int(result.split(',')[0])
y = int(result.split(',')[1])
xy_list = []
xy_list.append(x)
xy_list.append(y)
all_coorodinates.append(xy_list)
print(all_coorodinates)
return all_coorodinates def touch_click_words(self, coorodinates):
code_img = self.bro.find_element_by_class_name('touclick-img-par') for coorodinate in coorodinates:
x = coorodinate[0]
y = coorodinate[1]
ActionChains(self.bro).move_to_element_with_offset(code_img, x, y).click().perform() def login(self):
self.bro.find_element_by_id('username').send_keys(USERNAME)
self.bro.find_element_by_id('password').send_keys(PASSWORD)
self.bro.find_element_by_id('loginSub').click()
time.sleep(10) def run(self): # 1 打开浏览器
self.open_browser() # 2 找到并获取验证码图片
location,size = self.find_code_img()
self.get_code_img(location,size) # 3 识别验证码上字的坐标
all_coorodinates = self.get_words_locations() # 4 点击验证码图片上的字
self.touch_click_words(all_coorodinates) # 5 登录
self.login() if __name__ == "__main__":
hcp = Huochepiao()
hcp.run()

输出结果:

result: 186,86
[[186, 86]]

注意:本篇博文仅供学习交流相关的爬虫知识,请勿过度使用,如有任何纠纷,与本人无关。(瑟瑟发抖)

验证码破解 | Selenium模拟登陆12306的更多相关文章

  1. 验证码破解 | Selenium模拟登陆微博

    模拟登陆微博相对来说,并不难.验证码是常规的5个随机数字字母的组合,识别起来也比较容易.主要是用到许多Selenium中的知识,如定位标签.输入信息.点击等.如对Selenium的使用并不熟悉,请先移 ...

  2. 验证码破解 | Selenium模拟登录简书

    使用超级鹰打码平台处理登录的文字点击验证码 import time from io import BytesIO from PIL import Image from selenium import ...

  3. 验证码破解 | Selenium模拟登录知乎

      import requests import re import execjs import time import hmac from hashlib import sha1 class Zhi ...

  4. Selenium模拟登陆百度贴吧

    Selenium模拟登陆百度贴吧 from selenium import webdriver from time import sleep from selenium.webdriver.commo ...

  5. selenium 模拟登陆豆瓣,爬取武林外传的短评

    selenium 模拟登陆豆瓣,爬去武林外传的短评: 在最开始写爬虫的时候,抓取豆瓣评论,我们从F12里面是可以直接发现接口的,但是最近豆瓣更新,数据是JS异步加载的,所以没有找到合适的方法爬去,于是 ...

  6. 使用selenium模拟登陆新浪微博

    1.selenium基本使用 1.selenium安装及基本操作 selenium是一个自动化测试工具,它支持各种浏览器,包括Chrome,Safari,Firefox等主流界面浏览器驱动,也包括Ph ...

  7. 使用selenium模拟登陆淘宝、新浪和知乎

    如果直接使用selenium访问淘宝.新浪和知乎这些网址.一般会识别出这是自动化测试工具,会有反制措施.当开启开发者模式后,就可以绕过他们的检测啦.(不行的,哭笑) 如果网站只是对windows.na ...

  8. python selenium模拟登陆163邮箱。

    selenium是可以模拟浏览器操作. 有些爬虫是异步加载的,通过爬取网页源码是得不到需要的内容.所以可以模拟浏览器去登陆该网站进行爬取操作. 需要安装selenium通过pip install xx ...

  9. Python爬虫 —— 知乎之selenium模拟登陆获取cookies+requests.Session()访问+session序列化

    代码如下: # coding:utf-8 from selenium import webdriver import requests import sys import time from lxml ...

随机推荐

  1. Bootstrap:UI开发平台 sdk

    Bootstrap:UI开发平台 Bootstrap是一个易用.优雅.灵活.可扩展的前端工具包,里面包含了丰富的Web组件,包括布局.栅格.表格.表单.导航.按钮.进度条.媒体对象等,基于这些组件,可 ...

  2. <More Effective C#: 改善C#代码的50个有效方法>中文版翻译答疑

    最近, 有一本很赞的.NET技术书中文版出版了 - <More Effective C#: 改善C#代码的50个有效方法>.    从广州\西安\长沙\上海等各地.NET俱乐部都收到反馈, ...

  3. 打印出三位数的水仙花数Python

    水仙花数计算 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪ ...

  4. 基准测试工具:Wrk初识

    最近和同事聊起常用的一些压测工具,谈到了Apache ab.阿里云的PTS.Jmeter.Locust以及wrk各自的一些优缺点和适用的场景类型. 这篇博客,简单介绍下HTTP基准测试工具wrk的基本 ...

  5. Quartz基础调度框架-第二篇服务

    很多应用场景Quartz运行于Windows服务 Conf 在这个基本结构里 是用来存放配置  和上一篇 控制台运行的一样的结构 jobs.xml 的配置清单 <!-- 任务配置--> & ...

  6. 轨迹条(Trackbar)

    1.创建 createTrackbar(轨迹条名称,窗口名称,滑动条的初始位置,滑动条的最大值,回调函数XXX,不懂) 其中回调函数为 void XXX(滑动条的位置,用户数据)在c++中函数名为指向 ...

  7. winform+CefSharp 实现和js交互

    1:窗体加载的时候添加 webBrowser.RegisterJsObject("getuserName", new _Event()); 2:注册C#方法为js方法 /// // ...

  8. Vue--基础2

    目录 Vue--基础2 vue成员获取 分隔符成员 计算属性成员 什么是计算属性 计算属性的用法 注意: 监听属性成员 组件 组件的介绍 组件的优点: 局部组件 全局组件 组件复用的数据隔离 组件之间 ...

  9. 查看Linux内核版本

    您可能因多种原因需要确切知道GNU / Linux操作系统上运行的内核版本. 也许您正在调试与硬件相关的问题,或者了解影响旧内核版本的新安全漏洞,并且您想知道您的内核是否易受攻击. 无论是什么原因,从 ...

  10. WinExec, ShellExecute,CreateProcess 区别

    其中以WinExec最为简单,ShellExecute比WinExec灵活一些,CreateProcess最为复杂. WinExec 有两个参数,前一个指定路径,后一个指定显示方式. ShellExe ...