环境:python3
首先需要申请获得文字识别的API,例如:百度或者讯飞科技的文字识别API
核心代码:
# coding :gbk#APP ID:xxxxx#API KEY:aaaaa#SK:sssss#执行pip install baidu-aip 安装SDKfromaipimportAipOcrimportjsonimportseleniumimportwebdriver""" APPID AK SK """APP_ID='xxxxx'API_KEY='aaaaa'SECRET_KEY='sssss'client=AipOcr(APP_ID, API_KEY, SECRET_KEY) """ 读取图片 """defget_file_content(filePath): withopen(filePath, 'rb') asfp: returnfp.read() image=get_file_content(r'D:\image\1.jpg') #print(client.basicGeneral(image))textData=client.basicAccurate(image)['words_result'] text= [] print(textData) foriinrange(0, len(textData)): text.append(textData[i]['words']) text="".join(text) #"".join('%s' %id for id in text)#百度搜索:https://www.baidu.com/s?wd=link='https://www.baidu.com/s?wd='+text#headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36'}driver=webdriver.Chrome() driver.get(link) '''""" 如果有可选参数 """options = {}options["language_type"] = "CHN_ENG"options["detect_direction"] = "true"options["detect_language"] = "true"options["probability"] = "true"""" 带参数调用通用文字识别, 图片参数为本地图片 """client.basicGeneral(image, options)url = "https//www.x.com/sample.jpg"""" 调用通用文字识别, 图片参数为远程url图片 """client.basicGeneralUrl(url);""" 如果有可选参数 """options = {}options["language_type"] = "CHN_ENG"options["detect_direction"] = "true"options["detect_language"] = "true"options["probability"] = "true"""" 带参数调用通用文字识别, 图片参数为远程url图片 """client.basicGeneralUrl(url, options)'''