Hướng dẫn googletrans python limit

I have a small script making a request to http://translate.google.com/m?hl=%s&sl=%s&q=%s as my base_link however I've seemed to have surpassed the requests limit

HTTPError: HTTP Error 429: Too Many Requests

I am looking for what the limit is for google translate. The answers out there point to the Google Translate API limits however not the limit if one is directly accessing from Python's requests module.

Let me know if I can provide some more information.

asked Nov 7, 2020 at 20:12

Hướng dẫn googletrans python limit

3

The request you are submitting using the url http://translate.google.com/m?hl=%s&sl=%s&q=%s is using the free service via http://translate.google.com. The limits you have been seeing online is related to Google Translate API which is under Google Cloud Platform. These 2 are totally different from each other.

Currently, there is no public document discussing the limits in using googletrans. But, Googletrans will block your IP address once the system detects that you have been exploiting the free translation service by submitting huge amounts of requests, resulting in

HTTPError: HTTP Error 429: Too Many Requests

.

You can use Google Cloud Platform’s Translation API which is a paid service. Using this, you can avoid low limits set in googletrans which will give you more flexibility with the amount of requests you can submit.

answered Nov 9, 2020 at 6:45

Hướng dẫn googletrans python limit

According to this answer, the content quota (on the API) is 6,000,000 characters for 60 seconds per project and user. Therefore it is safe to assume that Google would protect the private API with much less than this. Google will not release the numbers for the private API (because it is supposed to only be used by the website itself, therefore they aren't willing to give any information about it since only they are supposed to know it anyways). If you need a more specific answer, I suggest you try and find more accurate API limits and assume it is much less than whatever number it is.

answered Nov 7, 2020 at 20:20

Hướng dẫn googletrans python limit

XiddocXiddoc

1,9703 gold badges8 silver badges25 bronze badges

I'm not sure but according to this source it seems to be 500,000 characters for free user.

answered Nov 7, 2020 at 20:29

Hướng dẫn googletrans python limit

1

Trong bài học này chúng ta sẽ tìm hiểu googletrans, hay còn gọi là Python Google Translator, đây là một module dùng để dịch văn bản qua các ngôn ngữ bằng cách sử dụng Google API.

Nội dung chính

  • 1. Python Google Translator là gì?
  • 2. Danh sách ngôn ngữ trong Python Google Translator module
  • 3. Dùng Python Google Translator nhận diện ngôn ngữ của text
  • 4. Dịch một đoạn text bằng Python Google Translator

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

1. Python Google Translator là gì?

Python googletrans là một module để dịch văn bản. Nó sử dụng API Google Translate Ajax để phát hiện ngôn ngữ và dịch văn bản sang ngôn ngữ mong muốn.

Trước khi sử dụng thì bạn phải kiểm tra trong máy của mình đã cài đặt module này chưa nhé, cách đơn giản nhất là chạy lệnh import sau đây:

Nếu lệnh này báo lỗi thì tức là máy của bạn chưa cài. Lúc này bạn hãy mở cửa sổ console lên và chạy lệnh sau:

Bài viết này được đăng tại [free tuts .net]

Giao diện cài đặt sẽ như sau:

Bạn hãy chạy lại lệnh import trên thì sẽ không thấy lỗi nữa.

2. Danh sách ngôn ngữ trong Python Google Translator module

Trước khi dịch sang một ngôn ngữ nào thì bạn nên kiểm tra xem Google API có hỗ trợ ngôn ngữ đó không.

Module googletrans có một thuộc tính tên là LANGUAGES, nó là một dict chứa danh sách các ngôn ngữ. Chạy lệnh sau để in ra.

import googletrans
print(googletrans.LANGUAGES)

Kết quả:

{'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', 'he': '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', 'or': 'odia', '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', 'ug': 'uyghur', 'uz': 'uzbek', 'vi': 'vietnamese', 'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', 'zu': 'zulu'}

3. Dùng Python Google Translator nhận diện ngôn ngữ của text

Google Translator có khả năng nhận diện ngôn ngữ của một đoạn text, đây chính là ưu điểm lớn nhất của bộ công cụ dịch thuật của Google.

Bây giờ mình thử detect một đoạn xem đó là ngôn ngữ gì nhé.

from googletrans import Translator

text = 'Học Python miễn phí tại freetuts.net'
translator = Translator()
dt = translator.detect(text)
print(dt)

Kết quả:

Detected(lang=vi, confidence=1.0)

4. Dịch một đoạn text bằng Python Google Translator

Để dịch một ngôn ngữ này sang một ngôn ngữ khác thì ta sử dụng đoạn code sau:

from googletrans import Translator

translator = Translator()

translated = translator.translate('svízelná situace', src='cs', dest='hu')

print(translated.text)

Trong đó:

  • src là ngôn ngữ ban đầu của đoạn text.
  • dest là ngôn ngữ cần dịch sang.
  • svízelná situace là đoạn text cần dịch.

Ngoài ra bạn cũng có thể truyền danh sách nhiều text cần dịch bằng cách đặt nó vào một mảng.

from googletrans import Translator

translator = Translator()

data = ['Dobrý deň', 'majestátny orol', 'krehká dohoda']

translated = translator.translate(data, src='sk', dest='en')

for trans in translated:
    print(f'{trans.origin} -> {trans.text}')

Trên là cách sử dụng module Google Translator trong Python. Bạn có thể tham khảo thêm nội dung trên trang chủ của Python pip nhé.