- 论坛徽章:
- 0
|
修改了下代码,不在命令行读取参数来翻译,而是根据输入raw_input来翻译。
附件提供了一个win编译的exe版本,用py2exe编译。
linux下直接输入文件名即可。
linux编码为utf-8,win编码为gbk
linux 代码:
- #!/usr/bin/python
- #coding:utf-8
- # *************************************************
- # author : smallfish <smallfish@live.cn>
- # http://hi.baidu.com/smallfish7788
- # date : 2008-08-28
- # version : 0.1
- # desc : 返回输入单词的中文翻译
- # *************************************************
- # 导入sys urllib re模块相应的常量和函数
- from sys import argv
- from urllib import urlopen
- from re import S, sub, compile
- # 格式化结果 去除换行、替换空格和html标签
- def format_html(s):
- s = sub("\s", '', s)
- s = sub("\s+", ' ', s)
- s = sub("<[^>]*>", '', s)
- return s
- # 发送查询
- def search_word(word) :
- searchurl = 'http://dict.yodao.com/search?tab=chn&keyfrom=dict.top&btnG=&q='
- html = urlopen(searchurl+str(word)).read()
- # 正则匹配结果
- results = compile('<td class="attributem1web">(.*?)</td>', S).findall(html)
- # 输出结果
- if not results :
- print '没有查询到结果'
- return
- for result in results :
- print format_html(result)
- print '# 这是一个用python写的翻译的小程序 作者:smallfish 邮件:smallfish@live.cn'
- print '# 输入需要翻译的单词 比如 >>> fish 输入q!退出本程序'
- input = raw_input('>>> ')
- while input != 'q!' :
- search_word(input)
- input = raw_input('>>> ')
复制代码
单词翻译最新版.rar
(1.55 MB, 下载次数: 262)
[ 本帖最后由 xiaoyu9805119 于 2008-9-5 09:30 编辑 ] |
|