我使用python3
,urllib3
和tika-server-1.13
以便從不同類型的文件中獲取文本。這是我的Python代碼:Python - urllib3使用tika服務器從docx獲取文本
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無法處理的實體 - 不支持的MIME類型,加密文件&等
這是正確的MIME類型(也蒂卡檢查它的檢測系統),它受到支持並且文件未加密。
我認爲這與我如何將文件上傳到tika服務器有關,我做錯了什麼?
我剛剛意識到這個問題是2歲。哎呀! – joefromct