【问题标题】:Googletrans throws a ConnectTimeout error (the handshake operation timed out)Googletrans 抛出 ConnectTimeout 错误(握手操作超时)
【发布时间】:2020-12-13 19:39:22
【问题描述】:

我目前正在使用 Python 3.8.5 开发翻译器,但没有得到合适的结果。 到目前为止我的代码是

from googletrans import Translator

text = '''Vous êtes français'''
translator = Translator()
dt = translator.detect(text)
print(dt)

在我的终端中,我应该获取源和目标 这是 Source 的 fr 和 dt 的 en 但我得到的是这个错误,我不知道如何修复。

Traceback (most recent call last):
  File "c:/Users/Akash/Desktop/Learning python/Project10-MegaProject-Translator.py", line 5, in <module>
    dt = translator.detect(text)
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\googletrans\client.py", line 255, in detect
    data = self._translate(text, 'en', 'auto', kwargs)
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\googletrans\client.py", line 78, in _translate
    token = self.token_acquirer.do(text)
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\googletrans\gtoken.py", line 194, in do
    self._update()
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\googletrans\gtoken.py", line 54, in _update
    r = self.client.get(self.host)
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpx\_client.py", line 755, in get
    return self.request(
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpx\_client.py", line 600, in request
    return self.send(
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpx\_client.py", line 620, in send
    response = self.send_handling_redirects(
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpx\_client.py", line 647, in send_handling_redirects
    response = self.send_handling_auth(
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpx\_client.py", line 684, in send_handling_auth
    response = self.send_single_request(request, timeout)
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpx\_client.py", line 714, in send_single_request
    ) = transport.request(
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpcore\_sync\connection_pool.py", line 152, in request
    response = connection.request(
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpcore\_sync\connection.py", line 65, in request
    self.socket = self._open_socket(timeout)
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpcore\_sync\connection.py", line 85, in _open_socket
    return self.backend.open_tcp_stream(
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpcore\_backends\sync.py", line 139, in open_tcp_stream
    return SyncSocketStream(sock=sock)
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpcore\_exceptions.py", line 12, in map_exceptions
    raise to_exc(exc) from None
httpcore._exceptions.ConnectTimeout: _ssl.c:1106: The handshake operation timed out
PS C:\Users\Akash\Desktop\Learning python> 

【问题讨论】:

标签: python google-translate


【解决方案1】:

您使用的googletrans API 与 translate.google.com 是同一个服务。不幸的是,如果您使用 googletrans 发送大量请求,您的 IP 将被 google 阻止。因为此服务并非旨在翻译大量数据。同样在documentation of googletrans API中提到了以下问题。

库使用注意事项

  1. 单个文本的最大字符数限制为 15k。
  2. 由于网页版谷歌翻译的限制,此API 不保证图书馆完全可以正常工作 次。 (所以如果你不关心,请使用这个库 稳定性。)
  3. 如果你想使用稳定的 API,我强烈推荐你使用 Google 的官方翻译 API。
  4. 如果您收到 HTTP 5xx 错误或类似 #6 的错误,可能是因为 Google 已禁止您的客户端 IP 地址。

我建议您使用official Translate API 来充分利用 API 的潜力。

【讨论】:

  • 所以你的意思是我的谷歌翻译不起作用,因为我正在发送大量数据进行翻译,我必须使用官方翻译 api,但我不知道该怎么做,就像从 google trans import 官方翻译 api 还是像导入其他东西一样,如果您添加一段代码来展示如何使用它,我将不胜感激。
  • @HellDenGaming 你可以查看这个关于如何设置Translate API 之后你可以试试这个quickstart。您可以浏览此site 以获取具有示例代码的其他指南。
【解决方案2】:

只需更改变量名称即可使用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 2018-08-24
    • 2020-04-11
    • 2021-04-19
    • 2018-06-29
    • 1970-01-01
    相关资源
    最近更新 更多