How to use the Python language to call the API of Google Translate

The gods are silent-personal CSDN blog post directory

This article uses the unofficial library pygoogletranslation https://pypi.org/project/pygoogletranslation/

This library is directly used by the web API of Google Translate's official website, so the service is unreliable.
The amount of translations is unlimited.

Download method:pip install pygoogletranslation

env_path/lib/python3.6/site-packages/pygoogletranslation/utils.pyLine 8 needs to be changed to: from pygoogletranslation.models import TranslatedPart1 (this seems to be a problem caused by moving from Python 2 to Python 3)

Call code:

from pygoogletranslation import Translator
proxy={
    
    "http":"127.0.0.1:7890","https":"127.0.0.1:7890"}
translator=Translator(proxies=proxy)
t=translator.translate(query,dest='zh-CN')
print(t.text)

For the agent part, read my medium blog post. This is a slide that cannot be touched.

The input parameters of the translate function:

  • query
  • The language that can be accepted by input and output can translator.glanguage()be viewed through variables
  • src: input language, autoauto-detect language by default
  • dest
    • zh-CN:Simplified Chinese

Other references used in writing this article:

  1. Google translation API-python interface-Googletrans_Google translation server url_PeterDanson's blog-CSDN blog

  1. ImportError: cannot import name ‘TranslatedPart’ from ‘googletrans.models’ · Issue #26 · Saravananslb/py-googletranslation ↩︎

Guess you like

Origin blog.csdn.net/PolarisRisingWar/article/details/131199334