先到百度云申请文字识别API ,会给你一个API KEY和一个SECRET KEY,免费,一天最多500次请求。
try: temp_url = ‘https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【API KEY】&client_secret=【SECRET KEY】‘ temp_res = requests.post(temp_url) temp_token = eval(temp_res.text)[‘access_token‘] temp_url = ‘https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=‘ + temp_token temp_headers = {‘Content-Type‘: ‘application/x-www-form-urlencoded‘} temp_file = open(‘code_xjtu.png‘, ‘rb‘) temp_image = temp_file.read() temp_file.close() temp_data = { ‘image‘: base64.b64encode(temp_image) } temp_data = urllib.parse.urlencode(temp_data) temp_res = requests.post(url=temp_url, data=temp_data, headers=temp_headers) code = int(eval(temp_res.text)[‘words_result‘][0][‘words‘])except Exception as e: print(e) print(‘验证码识别异常,请联系管理员‘)
原文地址:https://www.cnblogs.com/zealousness/p/8953714.html
时间: 2024-10-08 22:42:32