超级鹰的使用

超级鹰的使用流程:http://www.chaojiying.com/about.html
	- 注册(用户中心对应的身份账号)
	- 登陆
		- 获取一个软件id
		- 下载示例代码

超级鹰实现12306模拟登录

#!/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()


if __name__ == '__main__':
    # 用户中心>>软件ID 生成一个替换 96001
	chaojiying = Chaojiying_Client('bobo328410948', 'bobo328410948', '899370')	
    # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要
	im = open('a.jpg', 'rb').read()	
	print(chaojiying.PostPic(im, 1902)['pic_str'])										# 1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()
from selenium import webdriver
import time
import requests
from lxml import etree
from urllib import request
from selenium.webdriver import ActionChains
from PIL import Image

bro = webdriver.Chrome(executable_path='/Users/bobo/Desktop/oldBoy爬虫相关/chromedriver')
bro.get('https://kyfw.12306.cn/otn/login/init')

time.sleep(3)
#定位到验证码图片对应的img标签
code_img_ele = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img')
time.sleep(3)
#上一步定位到验证码图片标签的位置坐标(左上角的坐标)
location = code_img_ele.location  # x,y
#size返回的就是验证按摩图片的尺寸
size = code_img_ele.size #width and height
#rangle对应的就是验证码图片的裁剪区域
rangle = (int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height']))
#save_screenshot将当前浏览器打开的页面进行整体截图
bro.save_screenshot('aa.png')
#进行指定区域的截图
i = Image.open('./aa.png')
#验证码图片的名称
code_img_name = 'code.png'
#根据指定区域实现裁剪
frame = i.crop(rangle)
frame.save(code_img_name)
 
#将验证码图片提交给超级鹰进行识别
chaojiying = Chaojiying_Client('bobo328410948', 'bobo328410948', '899370')#用户中心>>软件ID 生成一个替换 96001
im = open('./code.png','rb').read()
result = chaojiying.PostPic(im, 9004)['pic_str']  
# "x1,y1|x2,y2" -->  [['x1','y1'],['x2','y2']]   "x,y"  --> [['x','y']]

all_list = []
if '|' in result:
    list_1 = result.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)
        all_list.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_list.append(xy_list)
print(all_list)

code_img = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img')

action = ActionChains(bro)
 
for l in all_list:
    x = l[0]
    y = l[1]
    ActionChains(bro).move_to_element_with_offset(code_img,x,y).click().perform()
 
bro.find_element_by_id('username').send_keys('xxxxxxx')
time.sleep(2)
bro.find_element_by_id('password').send_keys('xxxxxx')
time.sleep(2)
bro.find_element_by_id('loginSub').click()
time.sleep(10)
bro.quit()
posted @ 2019-06-27 10:25  言值  阅读(1958)  评论(0编辑  收藏  举报