µ±Ç°Î»Öà ²©ÎÄÊ×Ò³ > LYµÄ²©¿Í£º°áÔ˹ȸ跭ÒëGoogletrans python api

    LYµÄ²©¿Í£º°áÔ˹ȸ跭ÒëGoogletrans python api

    ×÷Õߣº[db:×÷Õß] ʱ¼ä£º2021-08-02 12:46

    Ô­ÎÄÍøÖ· :https://py-googletrans.readthedocs.io/en/latest/

    Googletrans is a?free?and?unlimited?python library that implemented Google Translate API. This uses the?Google Translate Ajax API?to make calls to such methods as detect and translate.

    Features

    • Fast and reliable - it uses the same servers that translate.google.com uses
    • Auto language detection
    • Bulk translations
    • Customizable service URL
    • Connection pooling (the advantage of using requests.Session)
    • HTTP/2 support

    Note on library usage

    • The maximum character limit on a single text is 15k.
    • Due to limitations of the web version of google translate, this API does not guarantee that the library would work properly at all times. (so please use this library if you don¡¯t care about stability.)
    • If you want to use a stable API, I highly recommend you to use?Google¡¯s official translate API.
    • If you get HTTP 5xx error or errors like #6, it¡¯s probably because Google has banned your client IP address.

    Quickstart

    You can install it from?PyPI:

    $ pip install googletrans
    

    HTTP/2 support

    This is a great deal for everyone! (up to 2x times faster in my test) If you want to get googletrans faster you should install?hyper?package. Googletrans will automatically detect if hyper is installed and if so, it will be used for http networking.

    Basic Usage

    If source language is not given, google translate attempts to detect the source language.

    >>> 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>
    

    Customize service URL

    You can use another google translate domain for translation. If multiple URLs are provided it then randomly chooses a domain.

    >>> from googletrans import Translator
    >>> translator = Translator(service_urls=[
          'translate.google.com',
          'translate.google.co.kr',
        ])
    

    Advanced Usage (Bulk)

    Array can be used to translate a batch of strings in a single method call and a single HTTP session. The exact same method shown above work for arrays as well.

    >>> 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  ->  ??? ?
    

    Language detection

    The detect method, as its name implies, identifies the language used in a given sentence.

    >>> translator.detect('? ??? ??? ??????.')
    # <Detected lang=ko confidence=0.27041003>
    >>> translator.detect('¤³¤ÎÎÄÕ¤ÏÈÕ±¾ÕZ¤Ç•ø¤«¤ì¤Þ¤·¤¿¡£')
    # <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 Guide

    googletrans.Translator

    class?googletrans.Translator(service_urls=None,?user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64)',?proxies=None,?timeout=None)

    Google Translate ajax API implementation class

    You have to create an instance of Translator to use this API

    Parameters:
    • service_urls?(a sequence of strings) ¨C google translate url list. URLs will be used randomly. For example?['translate.google.com',?'translate.google.co.kr']
    • user_agent?(str) ¨C the User-Agent header to send when making requests.
    • proxies?(dictionary) ¨C proxies configuration. Dictionary mapping protocol or protocol and host to the URL of the proxy For example?{'http':?'foo.bar:3128',?'http://host.name':?'foo.bar:4012'}
    • timeout?(number?or?a double of numbers) ¨C Definition of timeout for Requests library. Will be used by every request.

    translate(text,?dest='en',?src='auto')

    Translate text from source language to destination language

    Parameters:
    • text?(UTF-8?str;?unicode; string sequence (list, tuple, iterator, generator)) ¨C The source text(s) to be translated. Batch translation is supported via sequence input.
    • dest?¨C The language to translate the source text into. The value should be one of the language codes listed in?googletrans.LANGUAGES?or one of the language names listed in?googletrans.LANGCODES.
    • dest?¨C?str;?unicode
    • src?¨C The language of the source text. The value should be one of the language codes listed in?googletrans.LANGUAGES?or one of the language names listed in?googletrans.LANGCODES. If a language is not specified, the system will attempt to identify the source language automatically.
    • src?¨C?str;?unicode
    Return type:

    Translated

    Return type:

    list?(when a list is passed)

    Basic usage:

    >>> 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>
    

    Advanced usage:

    >>> 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  ->  ??? ?
    

    detect(text)

    Detect language of the input text

    Parameters:text?(UTF-8?str;?unicode; string sequence (list, tuple, iterator, generator)) ¨C The source text(s) whose language you want to identify. Batch detection is supported via sequence input.
    Return type:Detected
    Return type:list?(when a list is passed)

    Basic usage:

    >>> from googletrans import Translator
    >>> translator = Translator()
    >>> translator.detect('? ??? ??? ??????.')
    <Detected lang=ko confidence=0.27041003>
    >>> translator.detect('¤³¤ÎÎÄÕ¤ÏÈÕ±¾ÕZ¤Ç•ø¤«¤ì¤Þ¤·¤¿¡£')
    <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>
    

    Advanced usage:

    >>> langs = translator.detect(['???', 'ÈÕ±¾ÕZ', 'English', 'le fran?ais'])
    >>> for lang in langs:
    ...    print(lang.lang, lang.confidence)
    ko 1
    ja 0.92929292
    en 0.96954316
    fr 0.043500196
    

    googletrans.models

    class?googletrans.models.Translated(src,?dest,?origin,?text,?pronunciation,?extra_data=None)

    Translate result object

    Parameters:
    • src?¨C source langauge (default: auto)
    • dest?¨C destination language (default: en)
    • origin?¨C original text
    • text?¨C translated text
    • pronunciation?¨C pronunciation

    class?googletrans.models.Detected(lang,?confidence)

    Language detection result object

    Parameters:
    • lang?¨C detected language
    • confidence?¨C the confidence of detection result (0.00 to 1.00)

    googletrans.gtoken

    Hint

    This is for internal use only to generate a valid token to access translate.google.com ajax API.

    class?googletrans.gtoken.TokenAcquirer(tkk='0',?session=None,?host='translate.google.com')

    Google Translate API token generator

    translate.google.com uses a token to authorize the requests. If you are not Google, you do have this token and will have to pay for use. This class is the result of reverse engineering on the obfuscated and minified code used by Google to generate such token.

    The token is based on a seed which is updated once per hour and on the text that will be translated. Both are combined - by some strange math - in order to generate a final token (e.g. 744915.856682) which is used by the API to validate the request.

    This operation will cause an additional request to get an initial token from translate.google.com.

    Example usage:

    >>> from googletrans.gtoken import TokenAcquirer
    >>> acquirer = TokenAcquirer()
    >>> text = 'test'
    >>> tk = acquirer.do(text)
    >>> tk
    950629.577246
    

    googletrans.LANGUAGES

    Hint

    iso639-1 language codes for?supported languages?for translation. Some language codes also include a country code, like zh-CN or zh-TW.

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    LANGUAGES = {
        'af': 'afrikaans',
        'sq': 'albanian',
        'am': 'amharic',
        'ar': 'arabic',
        'hy': 'armenian',
        'az': 'azerbaijani',
        'eu': 'basque',
        'be': 'belarusian',
        'bn': 'bengali',
        'bs': 'bosnian',
        'bg': 'bulgarian',
        'ca': 'catalan',
        'ceb': 'cebuano',
        'ny': 'chichewa',
        'zh-cn': 'chinese (simplified)',
        'zh-tw': 'chinese (traditional)',
        'co': 'corsican',
        'hr': 'croatian',
        'cs': 'czech',
        'da': 'danish',
        'nl': 'dutch',
        'en': 'english',
        'eo': 'esperanto',
        'et': 'estonian',
        'tl': 'filipino',
        'fi': 'finnish',
        'fr': 'french',
        'fy': 'frisian',
        'gl': 'galician',
        'ka': 'georgian',
        'de': 'german',
        'el': 'greek',
        'gu': 'gujarati',
        'ht': 'haitian creole',
        'ha': 'hausa',
        'haw': 'hawaiian',
        'iw': 'hebrew',
        'hi': 'hindi',
        'hmn': 'hmong',
        'hu': 'hungarian',
        'is': 'icelandic',
        'ig': 'igbo',
        'id': 'indonesian',
        'ga': 'irish',
        'it': 'italian',
        'ja': 'japanese',
        'jw': 'javanese',
        'kn': 'kannada',
        'kk': 'kazakh',
        'km': 'khmer',
        'ko': 'korean',
        'ku': 'kurdish (kurmanji)',
        'ky': 'kyrgyz',
        'lo': 'lao',
        'la': 'latin',
        'lv': 'latvian',
        'lt': 'lithuanian',
        'lb': 'luxembourgish',
        'mk': 'macedonian',
        'mg': 'malagasy',
        'ms': 'malay',
        'ml': 'malayalam',
        'mt': 'maltese',
        'mi': 'maori',
        'mr': 'marathi',
        'mn': 'mongolian',
        'my': 'myanmar (burmese)',
        'ne': 'nepali',
        'no': 'norwegian',
        'ps': 'pashto',
        'fa': 'persian',
        'pl': 'polish',
        'pt': 'portuguese',
        'pa': 'punjabi',
        'ro': 'romanian',
        'ru': 'russian',
        'sm': 'samoan',
        'gd': 'scots gaelic',
        'sr': 'serbian',
        'st': 'sesotho',
        'sn': 'shona',
        'sd': 'sindhi',
        'si': 'sinhala',
        'sk': 'slovak',
        'sl': 'slovenian',
        'so': 'somali',
        'es': 'spanish',
        'su': 'sundanese',
        'sw': 'swahili',
        'sv': 'swedish',
        'tg': 'tajik',
        'ta': 'tamil',
        'te': 'telugu',
        'th': 'thai',
        'tr': 'turkish',
        'uk': 'ukrainian',
        'ur': 'urdu',
        'uz': 'uzbek',
        'vi': 'vietnamese',
        'cy': 'welsh',
        'xh': 'xhosa',
        'yi': 'yiddish',
        'yo': 'yoruba',
        'zu': 'zulu',
        'fil': 'Filipino',
        'he': 'Hebrew'
    }
    
    LANGCODES = dict(map(reversed, LANGUAGES.items()))
    
    cs