网络编程
位置:首页>> 网络编程>> Python编程>> python使用百度翻译进行中翻英示例

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

  发布时间:2021-04-08 20:10:11 

标签:python,翻译

利用百度词典进行中翻英


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('<br />',' ')
a=tran('word')#要翻译的词
ab = open('resault.txt','w')
ab.write(a)
ab.close()

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com