首页 > 解决方案 > Python - Google 自然语言处理 API 返回 HttpError 502 bad gateway

问题描述

我正在使用 Python(3.6) 和 Django(2) 开发一个项目,在该项目中我需要使用 Google 云自然语言处理 API 处理目录中的太多(~ 50k)文件,但在处理了 ~400 个文件后它返回错误为:

注意:我搜索了很多,所以请不要将其标记为重复!

引发 HttpError(resp, content, uri=self.uri)

googleapiclient.errors.HttpError: https://language.googleapis.com/v1beta2/documents:analyzeSentiment?alt=json 返回“错误网关”>

[2018 年 12 月 30 日 05:10:03]“POST/HTTP/1.1”500 15162

这里来自views.py

def nlp_text_manager(text_path, name):
    text = text_path
    # encoding = predict_encoding(text_path)
    # print('encoding is: {}'.format(encoding))
    txt = Path(text_path).read_text(encoding='utf8')
    service = discovery.build('language', 'v1beta2', credentials=credentials)
    service_request = service.documents().analyzeSentiment(
        body={
            'document': {
                'type': 'PLAIN_TEXT',
                'content': txt
            }
        }
    )
    response = service_request.execute()
    return response

标签: pythongoogle-cloud-pythonnatural-language-processinggoogle-natural-language

解决方案


推荐阅读