Python - urllib3 使用 tika 服务器从 docx 获取文本
Python - urllib3 get text from docx using tika server
我正在使用 python3
、urllib3
和 tika-server-1.13
来从不同类型的文件中获取文本。这是我的 python 代码:
def get_text(self, input_file_path, text_output_path, content_type):
global config
headers = util.make_headers()
mime_type = ContentType.get_mime_type(content_type)
if mime_type != '':
headers['Content-Type'] = mime_type
with open(input_file_path, "rb") as input_file:
fields = {
'file': (os.path.basename(input_file_path), input_file.read(), mime_type)
}
retry_count = 0
while retry_count < int(config.get("Tika", "RetriesCount")):
response = self.pool.request('PUT', '/tika', headers=headers, fields=fields)
if response.status == 200:
data = response.data.decode('utf-8')
text = re.sub("[\[][^\]]+[\]]", "", data)
final_text = re.sub("(\n(\t\r )*\n)+", "\n\n", text)
with open(text_output_path, "w+") as output_file:
output_file.write(final_text)
break
else:
if retry_count == (int(config.get("Tika", "RetriesCount")) - 1):
return False
retry_count += 1
return True
此代码适用于 html 文件,但当我尝试从 docx 文件解析文本时它不起作用。
我从服务器返回 Http 错误代码 422: Unprocessable Entity
使用 tika-server
documentation 我试过使用 curl
来检查它是否适用:
curl -X PUT --data-binary @test.docx http://localhost:9998/tika --header "Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document"
它奏效了。
422 Unprocessable Entity - Unsupported mime-type, encrypted document & etc
这是正确的 mime 类型(也用 tika 的检测系统检查过),它受支持并且文件未加密。
我认为这与我将文件上传到 tika 服务器的方式有关,我做错了什么?
您上传数据的方式不同。 --data-binary
in curl 只是按原样上传二进制数据。没有编码。在 urllib3 中,使用 fields
会导致 urllib3 生成 multipart/form-encoded
消息。最重要的是,您要阻止 urllib3 在请求中正确设置 header 以便 Tika 可以理解它。停止更新 headers['Content-Type']
或简单地传递 body=input_file.read()
.
我相信您可以通过将 tika-python 模块与 Client Only Mode 结合使用来简化此操作。
如果您仍然坚持使用自己的客户端,也许此模块的源代码中有一些线索可以显示他如何处理所有这些不同的 MIME 类型...如果您对 *.docx
你可能会与其他人有问题。
我正在使用 python3
、urllib3
和 tika-server-1.13
来从不同类型的文件中获取文本。这是我的 python 代码:
def get_text(self, input_file_path, text_output_path, content_type):
global config
headers = util.make_headers()
mime_type = ContentType.get_mime_type(content_type)
if mime_type != '':
headers['Content-Type'] = mime_type
with open(input_file_path, "rb") as input_file:
fields = {
'file': (os.path.basename(input_file_path), input_file.read(), mime_type)
}
retry_count = 0
while retry_count < int(config.get("Tika", "RetriesCount")):
response = self.pool.request('PUT', '/tika', headers=headers, fields=fields)
if response.status == 200:
data = response.data.decode('utf-8')
text = re.sub("[\[][^\]]+[\]]", "", data)
final_text = re.sub("(\n(\t\r )*\n)+", "\n\n", text)
with open(text_output_path, "w+") as output_file:
output_file.write(final_text)
break
else:
if retry_count == (int(config.get("Tika", "RetriesCount")) - 1):
return False
retry_count += 1
return True
此代码适用于 html 文件,但当我尝试从 docx 文件解析文本时它不起作用。
我从服务器返回 Http 错误代码 422: Unprocessable Entity
使用 tika-server
documentation 我试过使用 curl
来检查它是否适用:
curl -X PUT --data-binary @test.docx http://localhost:9998/tika --header "Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document"
它奏效了。
422 Unprocessable Entity - Unsupported mime-type, encrypted document & etc
这是正确的 mime 类型(也用 tika 的检测系统检查过),它受支持并且文件未加密。
我认为这与我将文件上传到 tika 服务器的方式有关,我做错了什么?
您上传数据的方式不同。 --data-binary
in curl 只是按原样上传二进制数据。没有编码。在 urllib3 中,使用 fields
会导致 urllib3 生成 multipart/form-encoded
消息。最重要的是,您要阻止 urllib3 在请求中正确设置 header 以便 Tika 可以理解它。停止更新 headers['Content-Type']
或简单地传递 body=input_file.read()
.
我相信您可以通过将 tika-python 模块与 Client Only Mode 结合使用来简化此操作。
如果您仍然坚持使用自己的客户端,也许此模块的源代码中有一些线索可以显示他如何处理所有这些不同的 MIME 类型...如果您对 *.docx
你可能会与其他人有问题。