跳转到主内容
新派编程网 - 新潮编程理念,全新技术课堂!

爬虫遇到验证码怎么办?用打码平台解决!

在爬虫的道路上,验证码是一道常见的关卡。简单的验证码我们可以手动解决,但复杂的验证码就需要借助一些外力了。今天,我们就来聊聊如何利用打码平台来应对验证码的挑战。

打码平台,顾名思义,就是专门提供验证码识别服务的平台。比如R若快(http://www.ruokuai.com/client)和云打码平台(http://www.yundama.com/price.html)等。下面,我们就以R若快为例,来看看如何使用打码平台来识别验证码。

使用R若快平台的步骤:

  • 注册两个用户:普通用户和开发者用户。
  • 截图验证码:截取网站上登录页面的验证码图片并保存到本地。
  • 调用打码平台接口:将验证码图片发送到打码平台并获取验证码结果。
#!/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.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):
        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):
        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(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'])

以上就是本文的全部内容,希望对大家的学习有所帮助。当然,除了R若快,还有很多其他的打码平台可以选择。如果你有其他的问题或者想法,欢迎在评论区留言讨论。

—— 林译川 / 新派编程网(www.xjpbg.com)

相关文章