在python中使用谷歌翻译获取"SSL certificate error"



我目前正在一个自动化项目上工作,并使用谷歌翻译将文本从一种语言转换为另一种语言。我已经在这个项目上工作了3-4个月了,但是最近我遇到了错误

"ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed:自签名证书链(_ssl.c:1129)">

我使用以下代码:

`from googletrans import Translator, constants
from pprint import pprint
trans=Translator()
translation=trans.translate(column_list,dest='en')`

下面是详细的错误:

Connect Error                              Traceback (most recent call last)Input In [155], in <cell line: 1>()----> 1 translation=trans.translate(column_list,dest='en')
File ~Anaconda3libsite-packagesgoogletransclient.py:205, in Translator.translate(self, text, dest, src, **kwargs)203 result = []204 for item in text:--> 205     translated = self.translate(item, dest=dest, src=src, **kwargs)206     result.append(translated)207 return result
File ~Anaconda3libsite-packagesgoogletransclient.py:210, in Translator.translate(self, text, dest, src, **kwargs)207     return result209 origin = text--> 210 data, response = self._translate(text, dest, src, kwargs)212 # this code will be updated when the format is changed.213 translated = ''.join([d[0] if d[0] else '' for d in data[0]])
File ~Anaconda3libsite-packagesgoogletransclient.py:108, in Translator._translate(self, text, dest, src, override)104 params = utils.build_params(client=self.client_type, query=text, src=src, dest=dest,105                             token=token, override=override)107 url = urls.TRANSLATE.format(host=self._pick_service_url())--> 108 r = self.client.get(url, params=params)110 if r.status_code == 200:111     data = utils.format_json(r.text)
File ~Anaconda3libsite-packageshttpx_client.py:755, in Client.get(self, url, params, headers, cookies, auth, allow_redirects, timeout)744 def get(745     self,746     url: URLTypes,(...)753     timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,754 ) -> Response:--> 755     return self.request(756         "GET",757         url,758         params=params,759         headers=headers,760         cookies=cookies,761         auth=auth,762         allow_redirects=allow_redirects,763         timeout=timeout,764     )
File ~Anaconda3libsite-packageshttpx_client.py:600, in Client.request(self, method, url, data, files, json, params, headers, cookies, auth, allow_redirects, timeout)575 def request(576     self,577     method: str,(...)588     timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,589 ) -> Response:590     request = self.build_request(591         method=method,592         url=url,(...)598         cookies=cookies,599     )--> 600     return self.send(601         request, auth=auth, allow_redirects=allow_redirects, timeout=timeout,602     )
File ~Anaconda3libsite-packageshttpx_client.py:620, in Client.send(self, request, stream, auth, allow_redirects, timeout)616 timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout)618 auth = self.build_auth(request, auth)--> 620 response = self.send_handling_redirects(621     request, auth=auth, timeout=timeout, allow_redirects=allow_redirects,622 )624 if not stream:625     try:
File ~Anaconda3libsite-packageshttpx_client.py:647, in Client.send_handling_redirects(self, request, auth, timeout, allow_redirects, history)644 if len(history) > self.max_redirects:645     raise TooManyRedirects()--> 647 response = self.send_handling_auth(648     request, auth=auth, timeout=timeout, history=history649 )650 response.history = list(history)652 if not response.is_redirect:
File ~Anaconda3libsite-packageshttpx_client.py:684, in Client.send_handling_auth(self, request, history, auth, timeout)682 request = next(auth_flow)683 while True:--> 684     response = self.send_single_request(request, timeout)685     if auth.requires_response_body:686         response.read()
File ~Anaconda3libsite-packageshttpx_client.py:714, in Client.send_single_request(self, request, timeout)705 transport = self.transport_for_url(request.url)707 try:708     (709         http_version,710         status_code,711         reason_phrase,712         headers,713         stream,--> 714     ) = transport.request(715         request.method.encode(),716         request.url.raw,717         headers=request.headers.raw,718         stream=request.stream,719         timeout=timeout.as_dict(),720     )721 except HTTPError as exc:722     # Add the original request to any HTTPError unless723     # there'a already a request attached in the case of724     # a ProxyError.725     if exc._request is None:
File ~Anaconda3libsite-packageshttpcore_syncconnection_pool.py:152, in SyncConnectionPool.request(self, method, url, headers, stream, timeout)149         logger.trace("reuse connection=%r", connection)151 try:--> 152     response = connection.request(153         method, url, headers=headers, stream=stream, timeout=timeout154     )155 except NewConnectionRequired:156     connection = None
File ~Anaconda3libsite-packageshttpcore_syncconnection.py:65, in SyncHTTPConnection.request(self, method, url, headers, stream, timeout)61     if not self.socket:62         logger.trace(63             "open_socket origin=%r timeout=%r", self.origin, timeout64         )---> 65         self.socket = self._open_socket(timeout)66     self._create_connection(self.socket)67 elif self.state in (ConnectionState.READY, ConnectionState.IDLE):
File ~Anaconda3libsite-packageshttpcore_syncconnection.py:85, in SyncHTTPConnection._open_socket(self, timeout)83 ssl_context = self.ssl_context if scheme == b"https" else None84 try:---> 85     return self.backend.open_tcp_stream(86         hostname, port, ssl_context, timeout87     )88 except Exception:89     self.connect_failed = True
File ~Anaconda3libsite-packageshttpcore_backendssync.py:139, in SyncBackend.open_tcp_stream(self, hostname, port, ssl_context, timeout)135 if ssl_context is not None:136     sock = ssl_context.wrap_socket(137         sock, server_hostname=hostname.decode("ascii")138     )--> 139 return SyncSocketStream(sock=sock)
File ~Anaconda3libcontextlib.py:137, in _GeneratorContextManager.exit(self, typ, value, traceback)135     value = typ()136 try:--> 137     self.gen.throw(typ, value, traceback)138 except StopIteration as exc:139     # Suppress StopIteration unless it's the same exception that140     # was passed to throw().  This prevents a StopIteration141     # raised inside the "with" statement from being suppressed.142     return exc is not value
File ~Anaconda3libsite-packageshttpcore_exceptions.py:12, in map_exceptions(map)10 for from_exc, to_exc in map.items():11     if isinstance(exc, from_exc):---> 12         raise to_exc(exc) from None13 raise
ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1129)"

我尝试在SSL和会话验证中创建未验证的上下文,但无法删除错误

import ssl
import urllib.request
context=ssl._create_unverified_context()
urllib.request.urlopen(trans, context=context)

"另一个solution"

session=requests.Session()
session.verify = False

任何帮助都是感激的。

感谢

我也遇到了同样的问题。有些时候会出错,有些时候不会。我找不到原因。但是禁用httpx库上的验证是有效的。这与您在会话中提出的类似。verify = False但在googletrans中进行了更改,以便您可以直接使用您使用的方式。

  1. 查找googletrans在您的计算机中的位置(例如C:Users{your_user}AppDataRoamingPythonPython310site-packagesgoogletrans)
  2. 开放client.py
  3. 内部init从Translator类更改这一行:
self.client = httpx.Client(http2=http2)

self.client = httpx.Client(http2=http2, verify=False)

相关内容

  • 没有找到相关文章

最新更新