Python 通过打码平台实现验证码的实现

yipeiwu_com5年前Python基础

 在爬虫时,经常遇到登录需要验证码的情况,简单的验证码可以自己解决,复制的验证码需要借助机器学习,有一定的难度。还有一个简单的方案就是采用付费的打码平台。

比如R若快(http://www.ruokuai.com/client),还有云打码平台(http://www.yundama.com/price.html)

下面以R若快为例介绍打码平台的思路:

R若快平台需要注册两个用户,一个是普通用户,一个开发者用户,

1、首先验证码截图,就是截取网站上登录页面的验证码图片并保存到本地

2、调用打码平台的接口把验证码图片发送到打码平台并获取到验证码结果。

具体代码如下:

#!/usr/bin/env python
# coding:utf-8

import requests
from hashlib import md5


class RClient(object):

  def __init__(self, username='shaoks123', password='123456', soft_id='113452', soft_key='c0d07d796c8e470c92a126df60d61794'):
    self.username = username
    # self.password = md5(password).hexdigest()
    self.password = md5(password.encode("utf-8")).hexdigest()
    self.soft_id = soft_id
    self.soft_key = soft_key
    self.base_params = {
      'username': self.username,
      'password': self.password,
      'softid': self.soft_id,
      'softkey': self.soft_key,
    }
    self.headers = {
      'Connection': 'Keep-Alive',
      'Expect': '100-continue',
      'User-Agent': 'ben',
    }

  def rk_create(self, im, im_type, timeout=60):
    """
    im: 图片字节
    im_type: 题目类型
    """
    params = {
      'typeid': im_type,
      'timeout': timeout,
    }
    params.update(self.base_params)
    files = {'image': ('a.jpg', im)}
    r = requests.post('http://api.ruokuai.com/create.json', data=params, files=files, headers=self.headers)
    return r.json()

  def rk_report_error(self, im_id):
    """
    im_id:报错题目的ID
    """
    params = {
      'id': im_id,
    }
    params.update(self.base_params)
    r = requests.post('http://api.ruokuai.com/reporterror.json', data=params, headers=self.headers)
    return r.json()

  def test(self,imagefile,im_type=1030):
    # im = open('E:\python36_crawl\Veriycode\code\code_823.png', 'rb').read()
    im = open(imagefile, 'rb').read()
    result = self.rk_create(im, im_type)
    print(result['Result'])
    return result['Result']


# if __name__ == '__main__':
#   rc = RClient()
#   im = open('E:\python36_crawl\Veriycode\code\code_823.png', 'rb').read()
#   result = rc.rk_create(im, 1030)
#   print(result['Result'])

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python实现可以断点续传和并发的ftp程序

前言 下载文件时,最怕中途断线,无法成功下载完整的文件。断点续传就是从文件中断的地方接下去下载,而不必重新下载。这项功能对于下载较大文件时非常有用。那么这篇文章就来给大家分享如何利用py...

python多线程操作实例

python多线程操作实例

一、python多线程 因为CPython的实现使用了Global Interpereter Lock(GIL),使得python中同一时刻只有一个线程在执行,从而简化了python解释...

基于python中staticmethod和classmethod的区别(详解)

例子 class A(object): def foo(self,x): print "executing foo(%s,%s)"%(self,x) @classm...

Python 自动刷博客浏览量实例代码

思路来源 今天很偶然的一个机会,听到别人在谈论现在的“刷量”行为,于是就激发了我的好奇心。然后看了下requests模块正好对我有用,就写了一个简单的测试用例。神奇的发现这一招竟然是管用...

python运用pygame库实现双人弹球小游戏

python运用pygame库实现双人弹球小游戏

使用python pygame库实现一个双人弹球小游戏,两人分别控制一个左右移动的挡板用来拦截小球,小球会在两板间不停弹跳,拦截失败的一方输掉游戏,规则类似于简化版的乒乓球。 因为是第一...