url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + get_access_token() # image 可以通过 get_file_content_as_base64("C:\fakepath\360截图20230601185021461.jpg",True) 方法获取 payload = 'image=%2F9j%2F4AAQ' headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' } response = requests.request("POST", url, headers=headers, data=payload) print(response.text)运行出现{"words_result":[{"words":"pip install tensorflow==1.12 -i https://mirrors.aliyun.com/pypi/simple","location":{"top":12,"left":9,"width":1401,"height":34}}],"words_result_num":1,"log_id":1667821169793475469},如何提取words中内容打印
时间: 2023-07-12 12:55:31 浏览: 61
可以使用 json 库对返回的 response.text 进行解析,然后提取出 words_result 中的 words 字段。代码如下:
```python
import json
# 发送 OCR 请求并获取响应
url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + get_access_token()
payload = 'image=%2F9j%2F4AAQ'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
# 解析响应并提取 words_result 中的 words 字段
result = json.loads(response.text)
words = result['words_result'][0]['words']
# 打印提取到的内容
print(words)
```
这样可以将提取到的文字内容打印出来。
相关问题
image_path = os.path.join(os.path.dirname(__file__), '11.jpg') # 读取图片二进制数据 with open(image_path, 'rb') as f: image_data = f.read() url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=" + get_access_token() # 构造请求头和请求体 # url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic' headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' } data = { 'image': image_data } payload = urllib.parse.urlencode(data) # 发送POST请求 response = requests.post(url, headers=headers, data=payload) # 解析响应并输出识别结果 result = response.json() words_result = result['words_result'] for words in words_result: print(words['words']),提示KeyError: 'words_result',如何修改
这个错误提示说明返回的 JSON 数据中没有 `words_result` 这个键值,因此不能通过 `result['words_result']` 获取该键对应的值。这可能是因为 OCR 识别没有成功,或者返回的 JSON 格式不同于预期。
你可以先打印输出整个 `result`,查看其中的内容和结构,看看是否能找到识别结果的内容。另外,你也可以尝试使用其他的 OCR 识别 API 或者其他工具,看看是否可以成功识别图片中的文字。
#!/usr/local/bin/python3 # -- coding: utf-8 -- # @Time : 2023/6/11 14:19 # @Author : 志在星空 # @File : jietu12.py # @Software: PyCharm import base64 import urllib import requests import json API_KEY = "jHxdMDf2bhEPxcG6zFoRUejo" SECRET_KEY = "QF5GO9eLQncG2Zr1MKrxLOWvKAkAtVfI" def main(): # 发送 OCR 请求并获取响应 url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + get_access_token() # payload = 'image=%2F9j%2F4AAQ' headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' } image_path = "11.jpg" with open(image_path, "rb") as image_file: image_data = image_file.read() response = requests.post(url, headers=headers, data=image_data) # response = requests.request("POST", url, headers=headers, data=payload) # 解析响应并提取 words_result 中的所有 words 字段 result = json.loads(response.text) print(result) # words_list = [item['words'] for item in result['words_result']] # print(words_list) # # # 打印所有提取到的内容 # for words in words_list: # print(words) def get_file_content_as_base64(path, urlencoded=False): """ 获取文件base64编码 :param path: 文件路径 :param urlencoded: 是否对结果进行urlencoded :return: base64编码信息 """ with open(path, "rb") as f: content = base64.b64encode(f.read()).decode("utf8") if urlencoded: content = urllib.parse.quote_plus(content) return content def get_access_token(): """ 使用 AK,SK 生成鉴权签名(Access Token) :return: access_token,或是None(如果错误) """ url = "https://aip.baidubce.com/oauth/2.0/token" params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY} return str(requests.post(url, params=params).json().get("access_token")) if name == 'main': main()运行出现{'log_id': 1667825949995168966, 'error_msg': 'param image not exist', 'error_code': 216101},请修改一下
根据你提供的代码和运行结果,可能是以下两个问题导致的:
1. 请求头中的 Content-Type 不正确,应该设置为 multipart/form-data,而不是 application/x-www-form-urlencoded。
2. 请求体中的数据格式不正确,应该是 multipart/form-data 格式,而不是 application/x-www-form-urlencoded 格式。
为了解决这个问题,可以尝试以下修改:
1. 修改请求头中的 Content-Type,将其设置为 multipart/form-data:
```
headers = {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json'
}
```
2. 修改请求体中的数据格式,将其设置为 multipart/form-data 格式。可以使用 Python 的 requests 库自动构建这种格式的请求体。修改后的代码如下:
```
import requests
import json
API_KEY = "jHxdMDf2bhEPxcG6zFoRUejo"
SECRET_KEY = "QF5GO9eLQncG2Zr1MKrxLOWvKAkAtVfI"
def main():
# 发送 OCR 请求并获取响应
url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + get_access_token()
image_path = "11.jpg"
with open(image_path, "rb") as image_file:
image_data = image_file.read()
response = requests.post(url, headers=headers, data={
'image': ('image.jpg', image_data)
})
result = json.loads(response.text)
print(result)
def get_access_token():
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
return str(requests.post(url, params=params).json().get("access_token"))
if __name__ == '__main__':
headers = {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json'
}
main()
```
这样就可以发送正确格式的 OCR 请求,并获取响应了。