本节目标:图片验证码识别 + 其他
1.图片验证码
在很多登录、注册、频繁操作等行为时,一般都会加入验证码的功能。
如果想要基于代码实现某些功能,就必须实现:自动识别验证码,然后再做其他功能。
2.识别
基于Python的模块 ddddocr
可以实现对图片验证码的识别。
1 2
| pip3.11 install ddddocr==1.4.9 -i https://mirrors.aliyun.com/pypi/simple/ pip3.11 install Pillow==9.5.0
|
1 2
| pip install ddddocr==1.4.9 -i https://mirrors.aliyun.com/pypi/simple/ pip install Pillow==9.5.0
|
2.1 本地识别
1 2 3 4 5 6 7
| import ddddocr
ocr = ddddocr.DdddOcr(show_ad=False) with open("img/v1.jpg", mode='rb') as f: body = f.read() code = ocr.classification(body) print(code)
|
2.2 在线识别
也可以直接请求获取图片,然后直接识别:
1 2 3 4 5 6 7 8
| import ddddocr import requests
res = requests.get(url="https://console.zbox.filez.com/captcha/create/reg?_t=1701511836608")
ocr = ddddocr.DdddOcr(show_ad=False) code = ocr.classification(res.content) print(code)
|
1 2 3 4 5 6 7 8 9 10 11
| import ddddocr import requests
res = requests.get( url=f"https://api.ruanwen.la/api/auth/captcha?captcha_token=n5A6VXIsMiI4MTKoco0VigkZbByJbDahhRHGNJmS" )
ocr = ddddocr.DdddOcr(show_ad=False) code = ocr.classification(res.content) print(code)
|
2.3 base64
有些平台的图片是以base64编码形式存在,需要处理下在识别。
1 2 3 4 5 6 7 8 9 10 11
| import base64 import ddddocr
content = base64.b64decode("iVBORw0KGgoAAAANSUhEUgAAAGQAAAAoCAYAAAAIeF9DAAAHGElEQVR4Xu2a2VNTZxTAHZ/62of+BX3rdPrUmaq1da3WQWur1mqntrQWLe7UkUoQlEWFqFDZZN8hUBWKQUVpQDCyVUeltVWIIiAEZHWBAEk4zffZe+bmS+6SEEzE/GbOkHvPuXeY85t7vyWZBV48ilnsCS/uxSvEw3hthJydXWITnsiMFyLWfLGcu5jRQuQ2W27dy8ArBOTXvQxmrBBHm+xo/XQhKkTffRu0NSfgt8KvISttGaQlfQyFOWtBXboDbt7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AABJRU5ErkJggg==")
ocr = ddddocr.DdddOcr(show_ad=False) code = ocr.classification(content) print(code)
|
3.案例:x文街
https://i.ruanwen.la/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import requests import ddddocr
res = requests.post(url="https://api.ruanwen.la/api/auth/captcha/generate") res_dict = res.json()
captcha_token = res_dict['data']['captcha_token'] captcha_url = res_dict['data']['src']
res = requests.get(captcha_url)
ocr = ddddocr.DdddOcr(show_ad=False) code = ocr.classification(res.content) print(code)
res = requests.post( url="https://api.ruanwen.la/api/auth/authenticate", json={ "mobile": "手机号", "device": "pc", "password": "密码", "captcha_token": captcha_token, "captcha": code, "identity": "advertiser" } )
print(res.json())
|
4.练习
https://hrtechchina.com/register
http://user.shangwuwang.com/public/register
https://apeclass.com/