selenium 12306模拟登陆
代码应用场景 :基于第三方打码网站模拟登陆12306
验证码识别
基于第三方平台超级鹰识别
超级鹰官网:http://www.chaojiying.com/user/
超级鹰使用流程:
注册 登陆(用户中心)充值
创建一个软件:软件ID->生成一个软件ID(901977)
下载实例代码->开发文档->python
1. 重新封装在打码平台下载到的python代码
#!/usr/bin/env python
# coding:utf-8 import requests
from hashlib import md5 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()
2.登陆12306代码逻辑
from selenium import webdriver
from selenium.webdriver import ActionChains
from CJY import Chaojiying_Client
from time import sleep
from PIL import Image #pip install Pillow def get_code_text(imgPath,imgType):
chaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '创建的软件ID') # 用户中心>>软件ID 生成一个替换 96001
im = open(imgPath, 'rb').read() # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
return chaojiying.PostPic(im, imgType) bro = webdriver.Chrome(executable_path='./chromedriver.exe')
bro.get('https://kyfw.12306.cn/otn/login/init')
sleep(2)
userName = bro.find_element_by_id('username')
userName.send_keys('12306账号')
sleep(2)
passWord = bro.find_element_by_id('password')
passWord.send_keys('12306密码') # bro.set_window_size(800,600)
bro.set_window_size(1920,1080) #验证码识别,实现点击操作
bro.save_screenshot('./main.png') #当前页面所对应的一整张图片 #验证码图片的标签
code_img_tag = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img') #验证码图片左下角坐标
location = code_img_tag.location #location返回的是位置
size = code_img_tag.size
# 裁剪区域
rangle = (int(location['x']),int(location['y']),int(location['x'])+size['width'],int(location['y']+size['height'])) i = Image.open('./main.png')
frame = i.crop(rangle)
frame.save('./code.png') img_text = get_code_text('./code.png',9004)
img_text = img_text['pic_str']
print(img_text,type(img_text)) all_list = []
if '|' in img_text:
list_1 = img_text.split('|')
count_1 = len(list_1)
for i in range(count_1):
xy_list = []
x = int(list_1[i].split(',')[0])
y = int(list_1[i].split(',')[1])
xy_list.append(x)
xy_list.append(y)
print(xy_list)
all_list.append(xy_list) else:
x = int(img_text.split(',')[0])
y = int(img_text.split(',')[1])
xy_list = []
xy_list.append(x)
xy_list.append(y)
all_list.append(xy_list) print(all_list)
for p in all_list:
x = p[0]
y = p[1]
# x ,y 就是即将要点击的坐标
ActionChains(bro).move_to_element_with_offset(code_img_tag,x,y).click().perform()
sleep(1) login_btn = bro.find_element_by_xpath('//*[@id="loginSub"]')
login_btn.click() sleep(5)
bro.quit()
ps: 如果不成功,最好是把自己电脑文字分辨率调整成100%
selenium 12306模拟登陆的更多相关文章
- 利用Python与selenium自动化模拟登陆12306官网!
近年来,12306的反爬越来越来严重,从一年前的 获取tk参数后到现在增加了 JS.CSS等加密方式! 目前大部分人利用的登陆方式都是利用selenium ,此文也不例外. 环境: Wi ...
- Python Selenium + phantomJS 模拟登陆教务管理系统 “抢课”
# _*_coding:utf-8_*_ from selenium import webdriver from selenium.webdriver.common.action_chains imp ...
- python selenium模拟登陆163邮箱。
selenium是可以模拟浏览器操作. 有些爬虫是异步加载的,通过爬取网页源码是得不到需要的内容.所以可以模拟浏览器去登陆该网站进行爬取操作. 需要安装selenium通过pip install xx ...
- 爬虫必知必会(4)_异步协程-selenium_模拟登陆
一.单线程+多任务异步协程(推荐) 协程:对象.可以把协程当做是一个特殊的函数.如果一个函数的定义被async关键字所修饰.该特殊的函数被调用后函数内部的程序语句不会被立即执行,而是会返回一个协程对象 ...
- Python爬虫(二十二)_selenium案例:模拟登陆豆瓣
本篇博客主要用于介绍如何使用selenium+phantomJS模拟登陆豆瓣,没有考虑验证码的问题,更多内容,请参考:Python学习指南 #-*- coding:utf-8 -*- from sel ...
- Python模拟登陆万能法-微博|知乎
Python模拟登陆让不少人伤透脑筋,今天奉上一种万能登陆方法.你无须精通HTML,甚至也无须精通Python,但却能让你成功的进行模拟登陆.本文讲的是登陆所有网站的一种方法,并不局限于微博与知乎,仅 ...
- Selenium&Pytesseract模拟登录+验证码识别
验证码是爬虫需要解决的问题,因为很多网站的数据是需要登录成功后才可以获取的. 验证码识别,即图片识别,很多人都有误区,觉得这是爬虫方面的知识,其实是不对的. 验证码识别涉及到的知识:人工智能,模式识别 ...
- Selenium&Pytesseract模拟登录+验证码识别
验证码是爬虫需要解决的问题,因为很多网站的数据是需要登录成功后才可以获取的. 验证码识别,即图片识别,很多人都有误区,觉得这是爬虫方面的知识,其实是不对的. 验证码识别涉及到的知识:人工智能,模式识别 ...
- 模拟登陆百度以及Selenium 的基本用法
模拟登陆百度,需要依赖于selenium 模块,调用浏览器,执行python命令 先来说一下这个selenium模块啦...... 本文参考内容来自 Selenium官网 SeleniumPython ...
随机推荐
- Visual Studio2013的C语言编译器对C99标准的支持情况
Visual Studio2013终于开始比较良好地支持C99特性了.在此之前,如果用C语言写代码的话,变量名都需要放到函数体的前面部分,代码写起来十分别扭. 而Visual Studio2013中的 ...
- Objective-C中的self与LLVM Clang新引入的instancetype
我们知道,大部分面向对象语言对于一个类的成员方法都有一个隐含的参数.在C++.Java.C#和JavaScript中是this,而在Objective-C中则是self.当然,由于Objective- ...
- [工具]tcping检查开放的端口
tcping小工具是一款用于tcp监控的软件.tcping小工具可以时刻监控服务器的网络情况,包括ping值和端口状态,可以突破机房和服务器的禁用设置,是一款十分实用的网络分析小工具. 下载地址:ht ...
- Visual Studio Code 上java开发环境搭建
在把一些开源的SDK中java代码转成C#代码时经常需要写点java代码来实验下功能,装个Eclipse或IDEAs吧,好像也不太值当,所以用vs code搭个环境偶尔来实验下.以下: 1.下载并装好 ...
- [Scikit-learn] 2.5 Dimensionality reduction - Probabilistic PCA & Factor Analysis
2.5.4. Factor Analysis PPCA的基本性质以及人肉推导: 以上假设z是标准正态分布的情况.以下是对z的分布的扩展,为general normal distribution. Fr ...
- uni-app 使用Vuex+ (强制)登录
一.在项目的根目录下新建一个store文件夹,然后在文件夹下新建一个index.js文件 二.在新建的index.js下引入vue和vuex,具体如下: //引入vue和vuex import Vue ...
- Spring Security(2):过滤器链(filter chain)的介绍
上一节中,主要讲了Spring Security认证和授权的核心组件及核心方法.但是,什么时候调用这些方法呢?答案就是Filter和AOP.Spring Security在我们进行用户认证以及授予权限 ...
- iOS——sqlite3的使用(iOS嵌入式关系数据库)
1>添加sqlite3动态库:libsqlite3.dylib,CoreGraphics.framework,UIKit.framework,Foundation.framework 2> ...
- web框架学习路线
0.配置 1.路由 2.view 3.model 4.序列化与反序列化. 5.auth&permission 6.header处理 7.http client
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...