爬虫进阶之验证码

yuyalinnb   ·   发表于 2022-11-12 23:02:02   ·   技术文章

0x01 验证码

1、图片验证码

1.1 什么是图片验证码?

阅读时间:三分中
验证码,CAPTCHA(全自动区分计算机和人类的图灵测试),就是看你是人还是脚本。

1.2 验证码的作用?

防止恶意破解密码,论坛灌水,刷票,有效防止暴力测试

1.3 使用场景?

注册
登录
频繁发送请求时

1.4 图片验证码处理方案

-手动输入 仅限于的登陆一次可以持续使用的情况
-图像识别引擎解析 一般使用ocr库(后面讲),效率低
-打码平台

2、图像识别引擎

2.1 tesseract介绍

是一个开源,免费的OCR(把文字识别成图像)引擎。

2.2 tesseract安装

要安装两个:windows的话,我把exe放在附件里,拿过去运行(绝对没有后门,只是为了方便用),安装后加入环境变量。
还有一个是python库,要安装pillow和pytesseract,要么在pycharm里加,要么cmd

  1. pip3 install pillow
  2. pip3 install pytesseract

2.3 引擎的使用

进入tesseract文件夹


cmd

输入

  1. tesseract.exe img.png result

其中img.png是你自己文件的名字
(im.png长这样)


然后我们在tesseract文件夹下得到了提取的结果
当然 你还可以cai result.txt 意思是查看里面的内容

  1. input

另一种方法,使用pytesseract里的image_to_string方法

  1. from PIL import Image#导入image类
  2. import pytesseract
  3. im = Image.open('test.png') #打开图片
  4. result = pytesseract.image_to_string(im) #把图片转化为字符串
  5. print(result)

同样也能得到

0x03 打码平台使用

3.1 为什么需要了解打码平台的使用

现在很多网站都会使用验证码来进行反爬,所以为了能够更好的获取数据,需要了解如何使用打码平台爬虫中的验证码

3.2 常见的打码平台

  1. 云打码:http://www.yundama.com/
  2. 能够解决通用的验证码识别
  3. 极验验证码智能识别辅助:http://jiyandoc.c2567.com/
  4. 能够解决复杂验证码的识别

3.3 云打码的使用

下面以云打码为例,了解打码平台如何使用

3.4 云打码官方接口

下面代码是云打码平台提供,做了个简单修改,实现了两个方法:

  1. indetify:传入图片的响应二进制数即可
  2. indetify_by_filepath:传入图片的路径即可识别

其中需要自己配置的地方是:

username = ‘whoarewe’ # 用户名

password = ‘*‘ # 密码

appid = 4283 # appid

appkey = ‘02074c64f0d0bb9efb2df455537b01c3’ # appkey

codetype = 1004 # 验证码类型

云打码官方提供的api如下:

  1. #yundama.py
  2. import requests
  3. import json
  4. import time
  5. class YDMHttp:
  6. apiurl = 'http://api.yundama.com/api.php'
  7. username = ''
  8. password = ''
  9. appid = ''
  10. appkey = ''
  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. def request(self, fields, files=[]):
  17. response = self.post_url(self.apiurl, fields, files)
  18. response = json.loads(response)
  19. return response
  20. def balance(self):
  21. data = {'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid,
  22. 'appkey': self.appkey}
  23. response = self.request(data)
  24. if (response):
  25. if (response['ret'] and response['ret'] < 0):
  26. return response['ret']
  27. else:
  28. return response['balance']
  29. else:
  30. return -9001
  31. def login(self):
  32. data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid,
  33. 'appkey': self.appkey}
  34. response = self.request(data)
  35. if (response):
  36. if (response['ret'] and response['ret'] < 0):
  37. return response['ret']
  38. else:
  39. return response['uid']
  40. else:
  41. return -9001
  42. def upload(self, filename, codetype, timeout):
  43. data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid,
  44. 'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)}
  45. file = {'file': filename}
  46. response = self.request(data, file)
  47. if (response):
  48. if (response['ret'] and response['ret'] < 0):
  49. return response['ret']
  50. else:
  51. return response['cid']
  52. else:
  53. return -9001
  54. def result(self, cid):
  55. data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid,
  56. 'appkey': self.appkey, 'cid': str(cid)}
  57. response = self.request(data)
  58. return response and response['text'] or ''
  59. def decode(self, filename, codetype, timeout):
  60. cid = self.upload(filename, codetype, timeout)
  61. if (cid > 0):
  62. for i in range(0, timeout):
  63. result = self.result(cid)
  64. if (result != ''):
  65. return cid, result
  66. else:
  67. time.sleep(1)
  68. return -3003, ''
  69. else:
  70. return cid, ''
  71. def post_url(self, url, fields, files=[]):
  72. # for key in files:
  73. # files[key] = open(files[key], 'rb');
  74. res = requests.post(url, files=files, data=fields)
  75. return res.text
  76. username = 'whoarewe' # 用户名
  77. password = '***' # 密码
  78. appid = 4283 # appid
  79. appkey = '02074c64f0d0bb9efb2df455537b01c3' # appkey
  80. filename = 'getimage.jpg' # 文件位置
  81. codetype = 1004 # 验证码类型
  82. # 超时
  83. timeout = 60
  84. def indetify(response_content):
  85. if (username == 'username'):
  86. print('请设置好相关参数再测试')
  87. else:
  88. # 初始化
  89. yundama = YDMHttp(username, password, appid, appkey)
  90. # 登陆云打码
  91. uid = yundama.login();
  92. print('uid: %s' % uid)
  93. # 查询余额
  94. balance = yundama.balance();
  95. print('balance: %s' % balance)
  96. # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果
  97. cid, result = yundama.decode(response_content, codetype, timeout)
  98. print('cid: %s, result: %s' % (cid, result))
  99. return result
  100. def indetify_by_filepath(file_path):
  101. if (username == 'username'):
  102. print('请设置好相关参数再测试')
  103. else:
  104. # 初始化
  105. yundama = YDMHttp(username, password, appid, appkey)
  106. # 登陆云打码
  107. uid = yundama.login();
  108. print('uid: %s' % uid)
  109. # 查询余额
  110. balance = yundama.balance();
  111. print('balance: %s' % balance)
  112. # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果
  113. cid, result = yundama.decode(file_path, codetype, timeout)
  114. print('cid: %s, result: %s' % (cid, result))
  115. return result
  116. if __name__ == '__main__':
  117. pass
用户名金币积分时间理由
Track-劲夫 50.00 0 2022-11-19 21:09:37 一个受益终生的帖子~~

打赏我,让我更有动力~

0 条回复   |  直到 2022-11-12 | 498 次浏览
登录后才可发表内容
返回顶部 投诉反馈

© 2016 - 2024 掌控者 All Rights Reserved.