Python Google Translate API,,参考自:http:/
Python Google Translate API,,参考自:http:/
参考自:http://www.icourse163.org/learn/BIT-1001870001?tid=1001962001#/learn/forumdetail?pid=1003366321
import requestsfrom 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 resultdef 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 resultdef 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 Google Translate API
相关内容
- 在Windows Python3.5 安装LightGBM,windows7系统安装教程,Ligh
- python3安装pwntools,,乌班图16.04上安
- python脚本修改hosts文件,python写脚本入门, 用python
- 鳄鱼岛 python暴力求解,,关键词: 题目解题思
- ???????????????????????? -Python???Python split()??????,python字符串
- mac命令行切换python版本,mac进入命令行模式,brew安装a
- Python资源,python教程百度云资源,1,安装Python
- python中不能写n++,python中%d, 初学pytho
- 用VS2019写第一个Python程序-Hello, Python!,,第一步: 打
- 基于redis(订阅发布)实现python和java进程间通信,,主要结
评论关闭