Identification codes 04

Identification codes 04

what is code?

It is a distinction between the user's public computer or automatic procedures. Code can be prevented: malicious crack the code, brush tickets, forum irrigation, effectively prevent a hacker to crack a particular way ever registered user login attempt with a specific program of violence, in fact, with a verification code is a lot of sites now prevailing way, we use relatively simple way to achieve this functionality.

Both good and bad code verification and reptiles

Sometimes, we want to climb to take some of the user based on some user information, such as crawling Zhang Weibo posts and the number of friends ...... so these times, we need to use some reptiles simulated landing platform, crawling relevant user information of the user after the successful landing. Simulation landing, often appear to obstruct the verification code, and we can not not deal with the verification code, otherwise it is impossible to simulate landing. So, let's look together, or how to deal with the identification code.

Coding cloud platform

Processing code, the most common way is to use a tripartite platform to help us verify the relevant code recognition and processing. A code which cloud platform is a very generic coding platform.

http://www.yundama.com/demo.html

img

The platform can help us identify the following types of code:

img

manual

  • Cloud coding platform:
    • Register (ordinary users and developers users) at the official website
    • Developer User login:
      • Download example code (development of the document - "call and the latest example of DLL-" PythonHTTP example download)
      • Create a software: my software - "Adding new software
      • Example code used in the source file to be modified, the value of the data allowed the identification codes in the picture

Case: ancient poetry network identification code

#YunCode.PY
import http.client, mimetypes, urllib, json, time, requests

######################################################################

import http.client, mimetypes, urllib, json, time, requests
class YDMHttp:
    apiurl = 'http://api.yundama.com/api.php'
    username = ''
    password = ''
    appid = ''
    appkey = ''
    def __init__(self, username, password, appid, appkey):
        self.username = username
        self.password = password
        self.appid = str(appid)
        self.appkey = appkey
    def request(self, fields, files=[]):
        response = self.post_url(self.apiurl, fields, files)
        response = json.loads(response)
        return response
    def balance(self):
        data = {'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey}
        response = self.request(data)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['balance']
        else:
            return -9001
    def login(self):
        data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey}
        response = self.request(data)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['uid']
        else:
            return -9001
    def upload(self, filename, codetype, timeout):
        data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)}
        file = {'file': filename}
        response = self.request(data, file)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['cid']
        else:
            return -9001
    def result(self, cid):
        data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey, 'cid': str(cid)}
        response = self.request(data)
        return response and response['text'] or ''
    def decode(self, filename, codetype, timeout):
        cid = self.upload(filename, codetype, timeout)
        if (cid > 0):
            for i in range(0, timeout):
                result = self.result(cid)
                if (result != ''):
                    return cid, result
                else:
                    time.sleep(1)
            return -3003, ''
        else:
            return cid, ''
    def report(self, cid):
        data = {'method': 'report', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey, 'cid': str(cid), 'flag': '0'}
        response = self.request(data)
        if (response):
            return response['ret']
        else:
            return -9001
    def post_url(self, url, fields, files=[]):
        for key in files:
            files[key] = open(files[key], 'rb');
        res = requests.post(url, files=files, data=fields)
        return res.text

def get_code_image(filename,code_type):
    '''
    调用次函数可识别验证码
    :param filename: 图片的本地路径
    :param code_type: 识别验证码的类型,可见云打码平台
    :return:
    '''
    result =None

    # 用户名
    username    = 'xxx'
    # 密码
    password    = 'xxx...'
	
    # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得!
    appid       = 10303

    # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得!
    appkey      = '5b435fc2b6666946f84d6cee324075f9'

    # 图片文件
    filename    = filename

    # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html
    codetype    = code_type

    # 超时时间,秒
    timeout     = 20

    # 检查
    if (username == 'username'):
        print('请设置好相关参数再测试')
    else:
        # 初始化
        yundama = YDMHttp(username, password, appid, appkey)

        # 登陆云打码
        uid = yundama.login();
        print('uid: %s' % uid)

        # 查询余额
        balance = yundama.balance();
        print('balance: %s' % balance)

        # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果
        cid, result = yundama.decode(filename, codetype, timeout);
        print('cid: %s, result: %s' % (cid, result))

    return result

#识别验证码.py
import requests
from lxml import etree
from YunCode import YDMHttp,get_code_image

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
}
url = 'https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx'
page_text = requests.get(url=url,headers=headers).text
#解析验证码图片地址
tree = etree.HTML(page_text)
code_img_src = 'https://so.gushiwen.org'+tree.xpath('//*[@id="imgCode"]/@src')[0]
#请求验证码图片,且保存至本地
img_data = requests.get(url=code_img_src,headers=headers).content
with open('./code.jpg','wb') as fp:
    fp.write(img_data)


get_code_image('./code.jpg',1004)

Guess you like

Origin www.cnblogs.com/hanfe1/p/12661464.html