Python Google翻译文本时发生错误,ConnectionError:HTTPSConnectionPool(host ='translate.google.com',port = 443)

时间:2018-11-21 05:33:32

标签: python google-translate google-translation-api

我想将一堆句子从越南语转换为英语,并保存在excel文件中。我大约有200句话。我拥有urllib3的最新版本。即使我只保留10个句子并在下面运行代码,也会出现以下错误。我一次可以翻译的句子数量有限制吗?

错误:

  

ConnectionError:HTTPSConnectionPool(host ='translate.google.com',端口= 443):url超过了最大重试次数:/(由NewConnectionError(':导致:无法建立新连接:[WinError 10060]尝试连接由于连接的一方在一段时间后未正确响应而失败,或者由于连接的主机未能响应而建立的连接失败',))

代码:

from googletrans import Translator
import pandas as pd
import numpy as np
import time
import math
translator = Translator()

data=pd.read_excel('C:\TextAnalytics\Vietnamese to english.xlsx',dtype=object)


data.to_csv('data_temp.csv')

unique_data=pd.DataFrame(data['Subreason'].value_counts()).reset_index()[['index']]

unique_data['Translations']=''

for i in range(len(unique_data)):
    temp=(unique_data['index'].iloc[i])
    if(type(temp)==str):
        print(temp)
        a=translator.translate(temp,src='vi',dest='en')
        unique_data['Translations'].iloc[i]=a.text
        print(a.text)
        if(i%20==0):
            time.sleep(5)
            print(i)
        else:
            time.sleep(1)

        if(i>20000):
            break

translate_dict=unique_data.set_index('index').to_dict().get('Translations')

data['Google_Translate']=data.REWORK_DESCRIPTION.map(translate_dict)

data.drop('Converted',inplace=True,axis=1)

path=r'data mv table.xls'
writer = pd.ExcelWriter(path, engine='openpyxl')
data.to_excel(writer, 'Translated', index=False)
writer.save()

0 个答案:

没有答案