python3項(xiàng)目中g(shù)oogletrans出現(xiàn)超時(shí)如何解決-創(chuàng)新互聯(lián)

本篇文章為大家展示了python3項(xiàng)目中g(shù)oogletrans出現(xiàn)超時(shí)如何解決,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括當(dāng)陽網(wǎng)站建設(shè)、當(dāng)陽網(wǎng)站制作、當(dāng)陽網(wǎng)頁制作以及當(dāng)陽網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,當(dāng)陽網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到當(dāng)陽省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

一. 問題:

在寫調(diào)用谷歌翻譯接口的腳本時(shí),老是報(bào)錯(cuò),我使用的的是googletrans這個(gè)模塊中Translator的translate方法,程序運(yùn)行以后會(huì)報(bào)訪問超時(shí)錯(cuò)誤:


Traceback (most recent call last): File "E:/PycharmProjects/MyProject/Translate/translate_test.py", line 3, in <module> result=translator.translate('?????.') File "D:\python3\lib\site-packages\googletrans\client.py", line 182, in translate data = self._translate(text, dest, src, kwargs) File "D:\python3\lib\site-packages\googletrans\client.py", line 78, in _translate token = self.token_acquirer.do(text) File "D:\python3\lib\site-packages\googletrans\gtoken.py", line 194, in do self._update() File "D:\python3\lib\site-packages\googletrans\gtoken.py", line 54, in _update r = self.client.get(self.host) File "D:\python3\lib\site-packages\httpx\_client.py", line 763, in get timeout=timeout, File "D:\python3\lib\site-packages\httpx\_client.py", line 601, in request request, auth=auth, allow_redirects=allow_redirects, timeout=timeout, File "D:\python3\lib\site-packages\httpx\_client.py", line 621, in send request, auth=auth, timeout=timeout, allow_redirects=allow_redirects, File "D:\python3\lib\site-packages\httpx\_client.py", line 648, in send_handling_redirects request, auth=auth, timeout=timeout, history=history File "D:\python3\lib\site-packages\httpx\_client.py", line 684, in send_handling_auth response = self.send_single_request(request, timeout) File "D:\python3\lib\site-packages\httpx\_client.py", line 719, in send_single_request timeout=timeout.as_dict(), File "D:\python3\lib\site-packages\httpcore\_sync\connection_pool.py", line 153, in request method, url, headers=headers, stream=stream, timeout=timeout File "D:\python3\lib\site-packages\httpcore\_sync\connection.py", line 65, in request self.socket = self._open_socket(timeout) File "D:\python3\lib\site-packages\httpcore\_sync\connection.py", line 86, in _open_socket hostname, port, ssl_context, timeout File "D:\python3\lib\site-packages\httpcore\_backends\sync.py", line 139, in open_tcp_stream return SyncSocketStream(sock=sock) File "D:\python3\lib\contextlib.py", line 130, in __exit__ self.gen.throw(type, value, traceback) File "D:\python3\lib\site-packages\httpcore\_exceptions.py", line 12, in map_exceptions raise to_exc(exc) from None httpcore._exceptions.ConnectTimeout: timed out

二. 解決方法:

 1.尋找解決方法

經(jīng)過多方資料查找,最后才知道google翻譯對(duì)接口進(jìn)行了更新,之前用的googletrans已經(jīng)不能用了。但是網(wǎng)上大神已經(jīng)開發(fā)出了新的方法

https://github.com/lushan88a/google_trans_new

在此道一聲感謝!

2.使用解決方法

在cmd中輸入以下指令即可。

pip install google_trans_new

三. 代碼(優(yōu)化)

from google_trans_new import google_translator
from multiprocessing.dummy import Pool as ThreadPool
import time
import re
"""
此版本調(diào)用新版google_trans_new
使用多線程訪問谷歌翻譯接口
能夠翻譯len(text)>5000的文本
"""
class Translate(object):
 def __init__(self):
 	#初始化翻譯文本路徑以及翻譯目標(biāo)語言
  self.txt_file='./test.txt'
  self.aim_language='zh-CN'
  
	#讀入要翻譯的文本文件
 def read_txt(self):
  with open(self.txt_file, 'r',encoding='utf-8')as f:
   txt = f.readlines()
  return txt
	
	#進(jìn)行文本處理,此為優(yōu)化
 def cut_text(self,text):
  #如果只是一行,就切割成5000字一次來翻譯
  if len(text)==1:
   str_text = ''.join(text).strip()
   #篩選是一行但是文本長(zhǎng)度大于5000
   if len(str_text)>5000:
    #使用正則表達(dá)式切割超長(zhǎng)文本為5000一段的短文本
    result = re.findall('.{5000}', str_text)
    return result
   else:
    #如果文本為一行但是這一行文本長(zhǎng)度小于5000,則直接返回text
    return text
   """
   如果不止一行,加以判斷
    (1)每行字符數(shù)都小于5000
   (2)有的行字符數(shù)小于5000,有的行字符數(shù)大于5000
   """
  else:
   result = []
   for line in text:
    #第(1)種情況
    if len(line)<5000:
     result.append(line)
    else:
     # 第(2)種情況,切割以后,追加到列表中
     cut_str=re.findall('.{5000}', line)
     result.extend(cut_str)
   return result

 def translate(self,text):
  if text:
   aim_lang = self.aim_language
   try:
	   t = google_translator(timeout=10)
	   translate_text = t.translate(text, aim_lang)
	   print(translate_text)
	   return translate_text
   except Exception as e:
    print(e)

def main():
 time1=time.time()
 #開啟八條線程
 pool = ThreadPool(8)
 trans = Translate()
 txt = trans.read_txt()
 texts = trans.cut_text(txt)
 try:
  pool.map(trans.translate, texts)
 except Exception as e:
  raise e
 pool.close()
 pool.join()
 time2 = time.time()
 print("一共翻譯了 {} 個(gè)句子,消耗了 {:.2f} s".format(len(texts),time2 - time1))

if __name__ == "__main__" :
 main()

上述內(nèi)容就是python3項(xiàng)目中g(shù)oogletrans出現(xiàn)超時(shí)如何解決,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁名稱:python3項(xiàng)目中g(shù)oogletrans出現(xiàn)超時(shí)如何解決-創(chuàng)新互聯(lián)
本文URL:http://www.muchs.cn/article45/ceedhi.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、營(yíng)銷型網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)、面包屑導(dǎo)航、網(wǎng)站制作標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化