Python Google Translate API
2017-04-04 23:08
435 查看
参考自:http://www.icourse163.org/learn/BIT-1001870001?tid=1001962001#/learn/forumdetail?pid=1003366321
import requests from bs4 import BeautifulSoup def getHTMLText(url): try: r = requests.get(url, timeout=30) r.raise_for_status() return r.text except: print("Get HTML Text Failed!") return 0 def google_translate_EtoC(to_translate, from_language="en", to_language="ch-CN"): #根据参数生产提交的网址 base_url = "https://translate.google.cn/m?hl={}&sl={}&ie=UTF-8&q={}" url = base_url.format(to_language, from_language, to_translate) #获取网页 html = getHTMLText(url) if html: soup = BeautifulSoup(html, "html.parser") #解析网页得到翻译结果 try: result = soup.find_all("div", {"class":"t0"})[0].text except: print("Translation Failed!") result = "" return result def google_translate_CtoE(to_translate, from_language="ch-CN", to_language="en"): #根据参数生产提交的网址 base_url = "https://translate.google.cn/m?hl={}&sl={}&ie=UTF-8&q={}" url = base_url.format(to_language, from_language, to_translate) #获取网页 html = getHTMLText(url) if html: soup = BeautifulSoup(html, "html.parser") #解析网页得到翻译结果 try: result = soup.find_all("div", {"class":"t0"})[0].text except: print("Translation Failed!") result = "" return result def main(): while True: inp = int(input("Chinese to Englisth is 1, English to Chinese is 2: ")) if inp == 1: words = input("请输入中文: ") print(google_translate_CtoE(words)) else: words = input("Please input English: ") print(google_translate_EtoC(words)) main()
相关文章推荐
- 【python】使用urllib,requests调google translate API
- python 调用Google Translate API 翻译
- asp.net Google的translate工具翻译 API
- Google Translate API of PHP GOOGLE翻译PHP 接口
- googleTranslateApi
- asp.net Google的translate工具翻译 API
- Google translate API 调用方法
- google diff python API 使用
- Google Translate API
- 使用 Google API 进行翻译的 Python 小工具
- 借助Google Translate API实现网站多国语言翻译功能
- 利用google的API获取世界城市经纬度(python实现)
- Goslate: Free Google Translate API
- Google App Engine ProtoRPC Python API 概述
- ASP Google的translate API代码
- Images, Users, URL Fetch Python API——Google App Engine Services简介
- Google App Engine ProtoRPC Python API 概述
- Google Translate API