本文介绍了Googletrans API AttributeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到"AttributeError:'NoneType'对象没有属性'group'"即使在 googletrans停止使用gtoken后仍会发生错误错误'NoneType'对象没有属性'group',但我却 __ init __()出现了意外的关键字参数'client'错误

I keep getting "AttributeError: 'NoneType' object has no attribute 'group' " error even after changing gtoken on googletrans stopped working with error 'NoneType' object has no attribute 'group' but I got __init__() got an unexpected keyword argument 'client' error instead

这是我的main.py

here my main.py

from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
import uvicorn
from googletrans import Translator
#init
app = FastAPI(debug=True)

templates = Jinja2Templates(directory="template")


#route
@app.get('/')
def home(request: Request):
    text = request.get('text')
    lang = request.get('lang')
    #print('text:',text,'lang:',lang)

    #connect the translator
    translator=Translator()

    #detect langguage
    dt = translator.detect(text)
    dt2 =dt.lang

    #translate the text
    translated = translator.translate(text, lang)
    tr =translated.text

    return templates.TemplateResponse({"request": request},"translates.html",{'translated':tr,'u_lang':dt2,'t_lang':lang})

#def translator(request):


if __name__=="__main__":
    uvicorn.run(app,host="127.0.0.1",port=8000)

,这里是我的translate.html在网站内执行翻译的地方

and here where my translate.html execute the translate within site

<form action="" method="get">
    <br>

<div class="form-input">
    <center><label for="TextareaInput">Enter Text </label></center>
    <center><textarea class="form-control" value="text" id="TextareaInput" rows="3"></textarea></center>
</div>
<div class="ui divider"></div>
<div class="form-selection">
  <center><label for="languages">Choose Langguage:</label></center>
  <center><select name="trans" id="languages">
    <option value="en">English</option>
    <option value="ms">Malay</option>
    <option value="zh-cn">Mandarin</option>
    <option value="ko">Korean</option>
      <option value="ja">Japanese</option>
      <option value="vi">Vietnamese</option>
      <option value="th">Thailand</option>
  </select></center>
</div>
<div class="ui divider"></div>
<div>
   <center> <button class="ui button">Translate</button></center>
</div>
<div class="ui divider"></div>
<div class="form-output">
    <div class="container">
        <br><br>
        <h1>Text succes translated {{u_lang}} to {{t_lang}}</h1>
        <center>
            <h1>{{translated}}</h1>
        </center>
    </div>
</div>
</form>

我已经撞墙了,因为这个错误不断弹出

I already hit wall because this error keep popping

推荐答案

几分钟前发布了带有修复程序的新alpha版本.

A new alpha version with a fix was released a few minutes ago.

按如下所示安装Alpha版本:

Install the alpha version like this:

pip install googletrans==3.1.0a0

要注意的重要事项:您必须指定服务网址,否则仍然会发生相同的错误.所以这应该工作:

Important thing to note:You have to specify a service url, otherwise the same error still occurs. So this should work:

from googletrans import Translator
translator = Translator(service_urls=['translate.googleapis.com'])
translator.translate("Der Himmel ist blau und ich mag Bananen", dest='en')

但是他仍然会返回错误(至少对我而言):

But his still returns the error (at least for me):

translator = Translator()
translator.translate("Der Himmel ist blau und ich mag Bananen", dest='en')

有关详细信息和更新,请参见此处的讨论: https://github.com/ssut/py-googletrans/pull/237

See the discussion here for details and updates: https://github.com/ssut/py-googletrans/pull/237

另请参阅以下讨论:

这篇关于Googletrans API AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:14