-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
125 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# coding: utf8 | ||
''' | ||
@Author: LCY | ||
@Contact: lchuanyong@126.com | ||
@blog: http://http://blog.csdn.net/lcyong_ | ||
@Date: 2018-01-15 | ||
@Time: 19:19 | ||
说明: appid和secretKey为百度翻译文档中自带的,需要切换为自己的 | ||
python2和python3部分库名称更改对应如下: | ||
httplib ----> http.client | ||
md5 ----> hashlib.md5 | ||
urllib.quote ----> urllib.parse.quote | ||
官方链接: | ||
http://api.fanyi.baidu.com/api/trans/product/index | ||
''' | ||
|
||
import http.client | ||
import hashlib | ||
import json | ||
import urllib | ||
import random | ||
import config | ||
|
||
|
||
def baidu_translate(content): | ||
appid = config.BaiduID | ||
secretKey = config.BaiduKEY | ||
httpClient = None | ||
myurl = '/api/trans/vip/translate' | ||
q = content | ||
fromLang = 'jp' # 源语言 | ||
toLang = 'zh' # 翻译后的语言 | ||
salt = random.randint(32768, 65536) | ||
sign = appid + q + str(salt) + secretKey | ||
sign = hashlib.md5(sign.encode()).hexdigest() | ||
myurl = myurl + '?appid=' + appid + '&q=' + urllib.parse.quote( | ||
q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str( | ||
salt) + '&sign=' + sign | ||
|
||
try: | ||
httpClient = http.client.HTTPConnection('api.fanyi.baidu.com') | ||
httpClient.request('GET', myurl) | ||
# response是HTTPResponse对象 | ||
response = httpClient.getresponse() | ||
jsonResponse = response.read().decode("utf-8") # 获得返回的结果,结果为json格式 | ||
js = json.loads(jsonResponse) # 将json格式的结果转换字典结构 | ||
dst = str(js["trans_result"][0]["dst"]) # 取得翻译后的文本结果 | ||
return dst # 打印结果 | ||
except Exception as e: | ||
print(e) | ||
finally: | ||
if httpClient: | ||
httpClient.close() | ||
|
||
|
||
if __name__ == '__main__': | ||
while True: | ||
print("请输入要翻译的内容,如果退出输入q") | ||
content = input() | ||
if (content == 'q'): | ||
break | ||
baidu_translate(content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
googletrans==2.4.0 | ||
httpx==0.12.1 | ||
emoji==0.5.4 | ||
nonebot==1.5.0 | ||
googletrans==2.4.0 | ||
requests==2.21.0 | ||
httpx==0.12.1 | ||
feedparser==5.2.1 | ||
apscheduler==3.6.3 | ||
Pillow==7.2.0 | ||
pyquery==1.4.1 | ||
retrying==1.3.3 |