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 ...
随机推荐
- 用jeecg做个项目第三讲(自定义导入导出)
1.导入 前端js和跳转页面 <t:dgToolBar title="导入单一模板" icon="icon-put" funname="Impo ...
- js es6遍历对象的6种方法(应用中推荐前三种)
javaScript遍历对象总结 1.for … in 循环遍历对象自身的和继承的可枚举属性(循环遍历对象自身的和继承的可枚举属性(不含Symbol属性).). 2.使用Object.keys ...
- 微信自定义分享 IOS端分享失败
1.在IOS微信浏览器中自定义分享link 链接中的中文需要encodeURIComponent() 编码(安卓会自动编码) 2.另外在IOS微信浏览器中自定义分享 imgUrl 不能大于34KB ...
- Linux命令之date
date [选项] [格式] date [-u|--utc|--universal] [MMDDhhmm[[CC]yy][.ss]] date命令根据指定格式显示当前时间或设置系统时间.很多shell ...
- Scrapy五大核心组件工作流程
一.Scrapy五大核心组件工作流程 1.核心组件 # 引擎(Scrapy) 对整个系统的数据流进行处理, 触发事务(框架核心). # 调度器(Scheduler) 用来接受引擎发过来的请求. 由过滤 ...
- Github上的python开源项目
Python开源项目,期待大家和我们一起共同维护 github排名榜单 https://github.com/trending github搜索榜单:https://github.com/search ...
- WePay-T
(需先申请微信支付商户账号) 在微信支付中绑定appid,公众号和小程序都一样 微信支付中如下: 微信公众平台如下(公众号与小程序一样): 微擎配置微信支付 appid.appsecret为公众号中对 ...
- Eureka报错: Connect to localhost:8761 timed out
最近整理配置Eureka时, 注册服务后, Eureka服务一直报出如下错误: 如下是我的单台eureka的 application.yml 配置: spring: application: name ...
- 一步步分析Java深拷贝的两种方式-clone和序列化
今天遇到一道面试题,询问深拷贝的两种方法.主要就是clone方法和序列化方法.今天就来分析一下这两种方式如何实现深拷贝.如果想跳过解析的朋友,直奔"重点来了!"寻找答案. clon ...
- Android开发环境搭建(studio版)
1.下载安装JDK 网址:http://www.oracle.com/technetwork/java/javase/downloads/index.html