Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement a Google Translation function in python

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to achieve a Google translation function in python, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Import requestsimport jsonimport reimport time

Def googleTranslate (text): "" translate content with Google and return translation result params: text translation content return: str s translation result ""

Url = 'https://translate.google.cn/_/TranslateWebserverUi/data/batchexecute?rpcids=MkEWBc&f.sid=-2984828793698248690&bl=boq_translate-webserver_20201221.17_p0&hl=zh-CN&soc-app=1&soc-platform=1&soc-device=1&_reqid=5445720&rt=c' headers = {' origin': 'https://translate.google.cn',' referer': 'https://translate.google.cn/',' sec-fetch-dest': 'empty' 'sec-fetch-mode': 'cors',' sec-fetch-site': 'same-origin',' user-agent': 'Mozilla/5.0 (Windows NT 10.0 Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36', 'x Mustang: 'CIW2yQEIpbbJAQjEtskBCKmdygEIrMfKAQj2x8oBCPfHygEItMvKAQihz8oBCNzVygEIi5nLAQjBnMsB',' Decoded':'message ClientVariations {repeated int32 variation_id = [3300101, 3300133, 3300164, 3313321, 3318700,3318774,3318775,3319220,331971333205403329163,3329601] Just in case, I added it all. Data = {'f. Requre: F'["MkEWBc", "[[\" {text}\\ ",\\" auto\\ ",\\" zh-CN\\ ", true], [null]], null," generic "]]'} # text is what you want to translate.

Res = requests.post (url, headers=headers, data=data). Text # gets the returned result pattern ='\)\]\}\'\ s *\ d {3jue 4}\ s *\ [(. *)\ s needs'# part part1 = re.findall (pattern) Res) part1_list = json.loads (part1 [0]) # string transfer list if part1_list [2] is None: # if there is no required data in the returned result Then return the input print (text) return text content1 = part1_list [2] .replace ('\ n\,') part2_list = json.loads (content1) [1] [0] [0] [5:] [0] # filter the repeated part of the result s =''for i in part2_list: # traverse every sentence in the result And splice s + = I [0] print (s) return stext = 'friend. Let us look back on 2020 and look forward to 2021.'print (text) googleTranslate (text)

Effect.

What's the effect?

How to translate into other languages?

Of course, this only implements the conversion of any language to Chinese, to another language, you can get the corresponding English letters of all languages by executing js, store them in the form of a dictionary, and finally save them as json files

Then, when executing the program, dynamically enter the translated language, go to the json file to get the corresponding English representative, and then replace the "zh-CN" in data. For example, if I want to translate into Japanese, I will replace "zh-CN" with "ja". In this way, the result of translation will be Japanese.

Modify data data

Translation result

After reading the above, do you have any further understanding of how to implement a Google translation function in python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report