一、有道翻译
1.1 实验环境
Anaconda2-4.3.1(Python2.7)
1.2 所需模块
①request
②json
1.3 一些细节
跟古根翻译同样的,那就是在中译英的过程中,如果碰到中文的句号或者感叹号等使段落结束的标点符号,那么就会切分翻译,最后以元组返回,我做了一个很蹩脚的处理,就是直接用split()进行符号分割,取回每一句话进行翻译,最后再串联起来,具体可以看我的另外一篇博文Python爬虫谷歌翻译。除此之外也可以自己看看返回的结构然后自己写个正则化取回翻译的内容。
1.4 代码
#-*- coding:utf-8 -*
import requests
import json
import re
def get_translate_data(word=None):
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
payload = {'type': 'AUTO', 'i': word, 'doctype': 'json', 'xmlVersion': 1.8,
'keyfrom': 'fanyi.web', 'ue': 'UTF-8', 'action': 'FY_BY_CLICKBUTTON',
'typoResult': 'true'
} # 建立数据字典
response = requests.post(url, data=payload)
# print response.text #返回字符串
content = json.loads(response.text) # 将字符串转换为json数据
return content['translateResult'][0][0]['tgt']
if __name__ == '__main__':
texts=get_translate_data('进行复杂性研究的必读入门书籍,原本以为这本书会跟普利高津的那本')
results=get_translate_data(texts)
print results
参考:http://m.blog.csdn.net/article/details?id=52900177&from=singlemessage