前言
使用有道翻译接口,将英文文章翻译成中文。
英语文章看不懂怎么办?没关系,教你如何把一篇英语文章转化为双语文章。
知识点:
- requests 的使用
- js 加密如何处理
- 英文文章变双语文章
开发环境:
- 解释器: Python 3.6.5 | Anaconda, Inc.
- 编辑器: pycharm 社区版
PS:如有需要Python学习资料的小伙伴可以私信“资料”
可以免费领取源码、项目实战视频、PDF文件等
部分代码
import random import timeimport requestsfrom hashlib import md5# 输入想要访问的网址post_url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' appVersion = "5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36" # 请求头参数headers = { "Accept": "application/json, text/javascript, */*; q=0.01", "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,zh;q=0.9", "Cache-Control": "no-cache", "Connection": "keep-alive", "Content-Length": "281", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Cookie": "OUTFOX_SEARCH_USER_ID=728181645@10.169.0.82; OUTFOX_SEARCH_USER_ID_NCOO=1392777521.250264; JSESSIONID=aaaapitSB3EHV64Y44Spx; ___rl__test__cookies=1597407303179", "Host": "fanyi.youdao.com", "Origin": "http://fanyi.youdao.com", "Pragma": "no-cache", "Referer": "http://fanyi.youdao.com/", "User-Agent": appVersion, "X-Requested-With": "XMLHttpRequest", if __name__ == '__main__': # while True: # word = input('请输入你想要的翻译的内容:') with open('文章内容.txt', mode='r', encoding='utf-8') as f: text = f.read() results = fanyi(text) r_s = results['translateResult'][0] with open('双语.txt', mode='w', encoding='utf-8') as f: for r in r_s: f.write(r['src']) f.write('\n') f.write(r['tgt']) f.write('\n') f.write('\n') print(r['src']) print(r['tgt']) # 制作成双语的文章
# 想要完整源码的同学可以关注我的公众号:松鼠爱吃饼干
# 回复“有道翻译”即可免费获取
效果如下