本篇文章为大家展示了python3项目中googletrans出现超时如何解决,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
一。问题:
在写调用谷歌翻译接口的脚本时,老是报的错,我使用的的是googletrans这个模块中译者的翻译方法,程序运行以后会报访问超时错误:
回溯(最近的电话最后):文件“E:/PycharmProjects/MyProject/翻译/translate_test.py",第3行,& lt; module>结果=translator.translate(& # 39; ? ? ? ? ? # 39;公司)文件“D: \ python3 \ lib \网站\ googletrans \ client.py", 182行,在翻译data=https://www.yisu.com/zixun/self._translate(文本、桌子、src, kwargs)文件“D:/python3/lib/网站/googletrans/客户端。py”, 78行,在_translate牌=self.token_acquirer.do(文本)文件“D:/python3/lib/网站/googletrans/gtoken。py”, 194行,在做self._update()文件”D:/python3/lib/网站/googletrans/gtoken。py”, 54, _update r=self.client.get (self.host)文件“D:/python3/lib/网站/httpx/_client。py”, 763行,在得到超时=超时,文件“D:/python3/lib/网站/httpx/_client。py”, 601行,在请求的请求,auth=auth allow_redirects=allow_redirects=超时,超时文件”D:/python3/lib/网站/httpx/_client。py”, 621行,在发送请求,auth=auth,超时=超时,allow_redirects=allow_redirects文件”D:/python3/lib/网站/httpx/_client。py”, 648行,在send_handling_redirects请求,auth=auth,超时=超时,历史=历史文件" D:/python3/lib/网站/httpx/_client。py”, 684行,send_handling_auth反应=自我。send_single_request(请求,超时)文件“D:/python3/lib/网站/httpx/_client。py”, 719行,send_single_request超时=timeout.as_dict(),文件“D:/python3/lib/网站/httpcore/_sync/connection_pool。py”, 153行,在请求方法,url,标题=头,流=流,超时=超时文件" D:/python3/lib/网站/httpcore _sync/连接。py”, 65行,在自我要求。套接字=self._open_socket(超时)文件“D:/python3/lib/网站/httpcore/_sync/连接。py”, 86行,在_open_socket主机名、端口、ssl_context,超时文件”D:/python3/lib/网站/httpcore _backends/同步。py”, 139行,在open_tcp_stream返回SyncSocketStream(袜子=袜子)文件”D:/python3/lib/contextlib。py”, 130行,在__exit__ self.gen。把文件(类型、价值、回溯)“D:/python3/lib/网站/httpcore/_exceptions。py”第12行,map_exceptions提高to_exc (exc) httpcore._exceptions。ConnectTimeout:超时
引用>二。解决方法:
, 1。寻找解决方法
经过多方资料查找,最后才知道谷歌翻译对接口进行了更新,之前用的googletrans已经不能用了。但是网上大神已经开发出了新的方法
https://github.com/lushan88a/google_trans_new引用>
在此道一声感谢!
2。使用解决方法
在cmd中输入以下指令即可。
pip安装google_trans_new
引用>三。代码(优化)
得到google_trans_new import google_translator 得到multiprocessing.dummy import  Pool as  ThreadPool import 时间 import 再保险 “““ 此版本调用最新版google_trans_new 使用多线程访问谷歌翻译接口 能够翻译len(文本)在5000年的文本 “““ class 翻译(对象): 自我,def __init__ (): ,#初始化翻译文本路径以及翻译目标语言 self.txt_file=& # 39;才能。/用法# 39; self.aim_language才能=& # 39;应用# 39; ,, #读入要翻译的文本文件 ,def read_txt(自我): with 才能;开放(self.txt_file, & # 39; " # 39;,编码=& # 39;utf - 8 # 39;) as f: ,,txt =, f.readlines () ,return 三种 #进行文本处理,此为优化 ,def cut_text(自我、文本): #才能如果只是一行,就切割成5000字一次来翻译 if 才能;len(文本)==1: ,,str_text =, & # 39; & # 39; . join(文本).strip () ,,#筛选是一行但是文本长度大于5000年 ,,if len (str_text)在5000年: ,,,#使用正则表达式切割超长文本为5000年一段的短文本 ,,,result =, re.findall (& # 39;。{5000} & # 39;,, str_text) ,,,return 结果 ,,: ,,,#如果文本为一行但是这一行文本长度小,于5000年则直接返回文本 ,,,return 文本 ,,,,, ,,如果不止一行,加以判断 ,,,(1)每行字符数都小于5000年 ,,(2)有的行字符数小于5000年,有的行字符数大于5000年 ,,,,, 其他的才能: ,,result =, [] ,,for line 拷贝文本: ,,,#第(1)种情况 ,,,if len(线)& lt; 5000: ,,,,result.append(线) ,,,: ,,,,#,第(2)种情况,切割以后,追加到列表中 ,,,,cut_str=re.findall(& # 39;。{5000} & # 39;,,行) ,,,,result.extend (cut_str) ,,return 结果 ,def 翻译(自我、文本): if 才能;文本: ,,aim_lang =self.aim_language ,,试一试: ,,t =, google_translator(超时=10) null null null null null null null null null null null null null null null null null null null null null null null nullpython3项目中googletrans出现超时如何解决