googletrans 报错解决方法【httpcore,JSONDecodeError】
googletrans json.decoder.JSONDecodeError: Extra data:
httpcore._exceptions.ConnectTimeout: timed out
完成以下两步:
1.使用 pip install google_trans_new https://blog.csdn.net/a1397852386/article/details/111479024 2.手动修改google_trans_new库一行代码 https://stackoverflow.com/questions/68214591/python-google-trans-new-translate-raises-error-jsondecodeerror-extra-data Change line 151 in google_trans_new/google_trans_new.py which is: response = (decoded_line + ']') to response = decoded_line # 首先初始化2条正样本语句,和两条负样本语句 p_sample1 = "酒店设施非常不错" p_sample2 = "这家价格便宜" n_sample1 = "拖鞋都发霉了,太差了" n_sample2 = "电视不好用,没有看到足球" # # 导入google翻译接口工具 # from googletrans import Translator # # 实例化翻译对象 # translator = Translator() # # # 先进行中文到韩文的翻译 # translations = translator.translate([p_sample1,p_sample2,n_sample1,n_sample2],dest='ko') # # # 获得翻译成韩文后的文本结果 # ko_result = list(map(lambda x: x.text, translations)) # # print("中间翻译结果韩文是:") # print(ko_result) # # # 接下来进行回译 # translations = translator.translate(ko_result,dest='zh-cn') # cn_result = list(map(lambda x: x.text,translations)) # print("回译得到的增强中文文本是:") # print(cn_result) from google_trans_new import google_translator # 实例化翻译对象 translator = google_translator(timeout=10) print([p_sample1,p_sample2,n_sample1,n_sample2]) translations = [] # 先进行中文到韩文的翻译 for text in [p_sample1,p_sample2,n_sample1,n_sample2]: translations.append(translator.translate(text, 'ko')) # 获得翻译成韩文后的文本结果 ko_result = translations print("中间翻译结果韩文是:") print(ko_result) # 接下来进行回译 translations = translator.translate(ko_result,'zh-cn') cn_result = translations print("回译得到的增强中文文本是:") print(cn_result)