googletrans 是一個免費的、可調用Google Translate API接口的python庫。它借助Google Translate Ajax API接口來實現文本的檢測和翻譯。
googletrans 的基本用法可以參考文檔連接:http://py-googletrans.rtfd.io/
API特性:
- 快速可靠 - 它使用translate.google.com使用的相同服務器
- 自動語言檢測
- 批量翻譯
- 可自定義的服務URL
- 連接池(使用requests.Session的優點)
- HTTP / 2支持
關於庫使用的注意事項:
- 單個文本的最大字符限制為15k。
- 由於谷歌翻譯的網頁版本的限制,此API不保證庫始終正常工作。 (如果您不關心穩定性,請使用此庫。)
- 如果您想使用穩定的API,我強烈建議您使用Google的官方翻譯API。
- 如果您收到HTTP 5xx錯誤或#6等錯誤,可能是因為Google已禁止您的客戶端IP地址。
快速開始:
您可以從PyPI安裝它:
$ pip install googletrans
HTTP / 2支持
這對每個人來說都很棒! (在我的測試中速度提高了2倍)如果你想更快地獲得googletrans,你應該安裝超級軟件包。 Googletrans會自動檢測是否安裝了hyper,如果安裝了hyper,它將用於http網絡。
基本用法
如果未提供源語言,Google翻譯會嘗試檢測源語言。
>>> from googletrans import Translator >>> translator = Translator() >>> translator.translate('안녕하세요.') # <Translated src=ko dest=en text=Good evening. pronunciation=Good evening.> >>> translator.translate('안녕하세요.', dest='ja') # <Translated src=ko dest=ja text=こんにちは。 pronunciation=Kon'nichiwa.> >>> translator.translate('veritas lux mea', src='la') # <Translated src=la dest=en text=The truth is my light pronunciation=The truth is my light>
自定義服務URL
您可以使用其他谷歌翻譯域進行翻譯。 如果提供了多個URL,則隨機選擇一個域。
>>> from googletrans import Translator >>> translator = Translator(service_urls=[ 'translate.google.com', 'translate.google.co.kr', ])
高級用法(批量)
數組可用於在單個方法調用和單個HTTP會話中轉換一批字符串。 上面顯示的完全相同的方法也適用於數組。
>>> translations = translator.translate(['The quick brown fox', 'jumps over', 'the lazy dog'], dest='ko') >>> for translation in translations: ... print(translation.origin, ' -> ', translation.text) # The quick brown fox -> 빠른 갈색 여우 # jumps over -> 이상 점프 # the lazy dog -> 게으른 개
語言檢測
顧名思義,檢測方法識別給定句子中使用的語言。
>>> translator.detect('이 문장은 한글로 쓰여졌습니다.') # <Detected lang=ko confidence=0.27041003> >>> translator.detect('この文章は日本語で書かれました。') # <Detected lang=ja confidence=0.64889508> >>> translator.detect('This sentence is written in English.') # <Detected lang=en confidence=0.22348526> >>> translator.detect('Tiu frazo estas skribita en Esperanto.') # <Detected lang=eo confidence=0.10538048>
API導航
googletrans.Translator
class googletrans.Translator(service_urls=None, user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64)', proxies=None, timeout=None)
谷歌翻譯ajax API實現類
您必須創建一個Translator實例才能使用此API
參數:
- service_urls(一系列字符串) - 谷歌翻譯網址列表。 網址將隨機使用。 例如['translate.google.com','translate.google.co.kr']
- user_agent(str) - 發出請求時要發送的User-Agent標頭。
- proxies(字典) - 代理配置。 字典映射協議或協議和主機到代理的URL例如{'http':'foo.bar:3128','http://host.name':'foo.bar:4012'}
- 超時(數字或數字的兩倍) - 請求庫的超時定義。 將被每個請求使用。
translate(text, dest='en', src='auto')
將文本從源語言翻譯為目標語言
參數:
- text(UTF-8 str; unicode;字符串序列(list,tuple,iterator,generator)) - 要翻譯的源文本。 通過序列輸入支持批量轉換。
- dest - 將源文本轉換為的語言。 該值應為googletrans.LANGUAGES中列出的語言代碼之一或googletrans.LANGCODES中列出的語言名稱之一。
- dest – str; unicode
- src - 源文本的語言。 該值應為googletrans.LANGUAGES中列出的語言代碼之一或googletrans.LANGCODES中列出的語言名稱之一。 如果未指定語言,系統將嘗試自動識別源語言。
- src – str; unicode
返回類型:Translated
返回類型:list (when a list is passed)
基本用法:
>>> from googletrans import Translator >>> translator = Translator() >>> data=translator.translate('이 문장은 한글로 쓰여졌습니다.',dest='zh-cn') >>> data.src 'ko' >>> data.dest 'zh-cn' >>> data.text '這句話是用韓文寫的。' >>>
高階用法:
>>> translations = translator.translate(['The quick brown fox', 'jumps over', 'the lazy dog'], dest='ko') >>> for translation in translations: ... print(translation.origin, ' -> ', translation.text) The quick brown fox -> 빠른 갈색 여우 jumps over -> 이상 점프 the lazy dog -> 게으른 개
返回類型class googletrans.models.
Translated
(src, dest, origin, text, pronunciation, extra_data=None)
屬性:
- src – source langauge (default: auto)
- dest – destination language (default: en)
- origin – original text
- text – translated text
- pronunciation – pronunciation
detect(text)
檢測輸入文本的語言
參數:
- text(UTF-8 str; unicode;字符串序列(list,tuple,iterator,generator)) - 要識別其語言的源文本。 通過序列輸入支持批量檢測。
返回類型:Detected
返回類型:list (when a list is passed)
基本用法:
>>> from googletrans import Translator
>>> translator = Translator() >>> translator.detect('이 문장은 한글로 쓰여졌습니다.') <Detected lang=ko confidence=0.27041003> >>> translator.detect('この文章は日本語で書かれました。') <Detected lang=ja confidence=0.64889508> >>> translator.detect('This sentence is written in English.') <Detected lang=en confidence=0.22348526> >>> translator.detect('Tiu frazo estas skribita en Esperanto.') <Detected lang=eo confidence=0.10538048>
高階用法:
>>> langs = translator.detect(['한국어', '日本語', 'English', 'le français'])
>>> for lang in langs: ... print(lang.lang, lang.confidence) ko 1 ja 0.92929292 en 0.96954316 fr 0.043500196
返回類型class googletrans.models.
Detected
(lang, confidence)
屬性:
- lang – detected language
- confidence – the confidence of detection result (0.00 to 1.00)