在写爬虫的时候,有不少网站有验证码,这一点大家可以自己动手训练,但费时费力,最省事的办法直接使用大平台提供的API,以百度为例。
去官网 https://console.bce.baidu.com/ 注册自己的账号。
创建文字识别的应用,获取三个参数:
APP_ID 、API_KEY 、SECRET_KEY
然后
pip install baidu-aip
官方有更为详细的教程 ,这里给出一个简单的识别本地图片验证码的代码。
from aip import AipOcr
def get_file_content(filepath):
with open(filepath, 'rb') as fp:
return fp.read()
def get_code_result():
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
image = get_file_content('captcha.jpg')
# 识别结果
api_result = client.basicGeneral(image)
print(api_result)
code = api_result['words_result'][0]['words']
return code
网友评论