Home >Backend Development >Python Tutorial >python使用百度翻译进行中翻英示例

python使用百度翻译进行中翻英示例

WBOY
WBOYOriginal
2016-06-16 08:44:311714browse

利用百度词典进行中翻英

复制代码 代码如下:

import urllib2
import re
import sys


reload(sys)
sys.setdefaultencoding('utf-8')
def tran(word):
    url='http://dict.baidu.com/s?wd={0}&tn=dict'.format(word)
    print url
    req=urllib2.Request(url)
    resp=urllib2.urlopen(req)
    resphtml=resp.read()
    text = re.search(r'explain: "(.*)"',resphtml)
    return text.group(1).replace('
',' ')
a=tran('word')#要翻译的词
ab = open('resault.txt','w')
ab.write(a)
ab.close()

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn