zoukankan      html  css  js  c++  java
  • 验证码处理 -- 爬虫

    引入

    • 相关的门户网站在进行登录的时候,如果用户连续登录的次数超过3次或者5次的时候,就会在登录页中动态生成验证码。通过验证码达到分流和反爬的效果
    • - 1.对携带验证码的页面数据进行抓取
    • - 2.可以将页面数据中验证码进行解析,验证码图片下载到本地
    • - 3.可以将验证码图片提交给三方平台进行识别,返回验证码图片上的数据值
    • - 云打码平台:(具体云代码平台接口代码见百度云,链接: https://pan.baidu.com/s/1utwt70743u2PJP4BErq3ZQ 提取码: kwtu 复制这段内容后打开百度网盘手机App,操作更方便哦)

    具体实现代码如下:

     1 import http.client, mimetypes, urllib, json, time, requests
     2 
     3 
     4 class YDMHttp:
     5     apiurl = 'http://api.yundama.com/api.php'
     6     username = ''
     7     password = ''
     8     appid = ''
     9     appkey = ''
    10 
    11     def __init__(self, username, password, appid, appkey):
    12         self.username = username
    13         self.password = password
    14         self.appid = str(appid)
    15         self.appkey = appkey
    16 
    17     def request(self, fields, files=[]):
    18         response = self.post_url(self.apiurl, fields, files)
    19         response = json.loads(response)
    20         return response
    21 
    22     def balance(self):
    23         data = {'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid,
    24                 'appkey': self.appkey}
    25         response = self.request(data)
    26         if (response):
    27             if (response['ret'] and response['ret'] < 0):
    28                 return response['ret']
    29             else:
    30                 return response['balance']
    31         else:
    32             return -9001
    33 
    34     def login(self):
    35         data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid,
    36                 'appkey': self.appkey}
    37         response = self.request(data)
    38         if (response):
    39             if (response['ret'] and response['ret'] < 0):
    40                 return response['ret']
    41             else:
    42                 return response['uid']
    43         else:
    44             return -9001
    45 
    46     def upload(self, filename, codetype, timeout):
    47         data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid,
    48                 'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)}
    49         file = {'file': filename}
    50         response = self.request(data, file)
    51         if (response):
    52             if (response['ret'] and response['ret'] < 0):
    53                 return response['ret']
    54             else:
    55                 return response['cid']
    56         else:
    57             return -9001
    58 
    59     def result(self, cid):
    60         data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid,
    61                 'appkey': self.appkey, 'cid': str(cid)}
    62         response = self.request(data)
    63         return response and response['text'] or ''
    64 
    65     def decode(self, filename, codetype, timeout):
    66         cid = self.upload(filename, codetype, timeout)
    67         if (cid > 0):
    68             for i in range(0, timeout):
    69                 result = self.result(cid)
    70                 if (result != ''):
    71                     return cid, result
    72                 else:
    73                     time.sleep(1)
    74             return -3003, ''
    75         else:
    76             return cid, ''
    77 
    78     def report(self, cid):
    79         data = {'method': 'report', 'username': self.username, 'password': self.password, 'appid': self.appid,
    80                 'appkey': self.appkey, 'cid': str(cid), 'flag': '0'}
    81         response = self.request(data)
    82         if (response):
    83             return response['ret']
    84         else:
    85             return -9001
    86 
    87     def post_url(self, url, fields, files=[]):
    88         for key in files:
    89             files[key] = open(files[key], 'rb')
    90         res = requests.post(url, files=files, data=fields)
    91         return res.text
    封装一个类YDMHttp

    继续封装一个函数,调用这个类并执行其中的代码:

     1 def parse_code_img(username, password, appid, appkey, filename, codetype):
     2     # 用户名
     3     username = username
     4     # 密码
     5     password = password
     6     # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得!
     7     appid = appid
     8     # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得!
     9     appkey = appkey
    10     # 图片文件
    11     filename = filename
    12     # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html
    13     codetype = codetype
    14     # 超时时间,秒
    15     timeout = 30
    16 
    17     # 检查
    18     if (username == ''):
    19         print('请设置好相关参数再测试')
    20     else:
    21         # 初始化
    22         yundama = YDMHttp(username, password, appid, appkey)
    23         # 登陆云打码
    24         uid = yundama.login()
    25         print('uid: %s' % uid)
    26         # 查询余额
    27         balance = yundama.balance()
    28         print('balance: %s' % balance)
    29         # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果
    30         cid, result = yundama.decode(filename, codetype, timeout)
    31         print('cid: %s, result: %s' % (cid, result))
    32         return result
    parse_code_img

    之后哪里需要验证码处理将此两段代码复制到一个文件里,调用此文件,执行parse_code_img方法即可获取验证码的文本信息

  • 相关阅读:
    尝试实现一个简单的C语言string类型
    LeetCode.49
    Python学习 —— 实现简单的爬虫
    图表可视化表达的逻辑原理
    颜色参数
    Python交互图表可视化Bokeh:7. 工具栏
    Python交互图表可视化Bokeh:6. 轴线| 浮动| 多图表
    Python交互图表可视化Bokeh:5 柱状图| 堆叠图| 直方图
    Python交互图表可视化Bokeh:4. 折线图| 面积图
    Python交互图表可视化Bokeh:3. 散点图
  • 原文地址:https://www.cnblogs.com/wj12312/p/10108492.html
Copyright © 2011-2022 走看看