2025年3月27日 星期四 甲辰(龙)年 月廿六 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

python爬虫:第四章 在(1)验证码识别

时间:11-05来源:作者:点击数:44

第四章 验证码识别

1.验证码识别

验证码和爬虫之间的爱恨情仇?

1.1、反爬机制:验证码.识别验证码图片中的数据,用于模拟登陆操作。

识别验证码的操作:

  • 人工肉眼识别。(不推荐)
  • 第三方自动识别(推荐)
    • 第三方库:tesserocr
    • 看csnd博主:「 25’ h 」
    • 超级鹰:https://www.chaojiying.com/user/
    • 云打码:http://www.yundama.com/demo.html
      云打码的使用流程:
      • 注册:普通和开发者用户
      • 登录:
    • 普通用户的登录:查询该用户是否还有剩余的题分
    • 开发者用户的登录:
      • 创建一个软件:我的软件-》添加新软件-》录入软件名称-》提交(软件id和秘钥)
      • 下载示例代码:开发文档-》点此下载:云打码接口DLL-》PythonHTTP示例下载 -》解压后,代码的文档中

2、实战:识别古诗文网登录页面中的验证码

古诗文网址:https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx

使用打码平台识别验证码的编码流程:

  • 将验证码图片进行本地下载

    • 调用平台提供的示例代码进行图片数据识别

1.请求过程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oAy3qbdP-1637062720658)(图片/1.png)]

1.古诗文网验证识别.py

  • import requests
  • from lxml import etree
  • from CodeClass import YDMHttp
  • #封装识别验证码图片的函数
  • def getCodeText(imgPath,codeType):
  • # 普通用户用户名
  • username = 'bobo328410948'
  • # 普通用户密码
  • password = 'bobo328410948'
  • # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得!
  • appid = 6003
  • # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得!
  • appkey = '1f4b564483ae5c907a1d34f8e2f2776c'
  • # 图片文件:即将被识别的验证码图片的路径
  • filename = imgPath
  • # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html
  • codetype = codeType
  • # 超时时间,秒
  • timeout = 20
  • result = None
  • # 检查
  • 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
  • #将验证码图片下载到本地
  • 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
  • #解析验证码图片img中src属性值
  • 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)
  • #调用打码平台的示例程序进行验证码图片数据识别
  • code_text = getCodeText('code.jpg',1004)
  • print('识别结果为:',code_text)

2、CodeClass.py (这是那个云打码的)

  • 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
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门