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

yipeiwu_com6年前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()

相关文章

python matplotlib实现双Y轴的实例

python matplotlib实现双Y轴的实例

如下所示: import matplotlib.pyplot as plt import numpy as np x = np.arange(0., np.e, 0.01) y1...

django 自定义过滤器(filter)处理较为复杂的变量方法

简述:django 在views中有数据需要通过字典(dict)的方式传递给template,该字典中又包含了字典,而且字典中的键值还是一个对象,在template中处理传递过来的数据的...

TensorFlow安装及jupyter notebook配置方法

tensorflow利用anaconda在ubuntu下安装方法及jupyter notebook运行目录及远程访问配置 Ubuntu下安装Anaconda bash ~/file_...

Python中函数的返回值示例浅析

前言: 前面我们介绍了简单的介绍了函数和函数的参数,今天我们来说一下Python中函数的返回值。 函数的返回值:函数运算的结果,需要进一步的操作时,给一个返回值return用来返回函数...

浅谈python函数调用返回两个或多个变量的方法

以元祖形式返回  return (a,b,......) 以元祖引用或(x,y,....)接受都可以 为什么不能用列表返回?? 与java一样,列表等属于可变数据类型——由指针...