python 單詞翻譯小工具

附件提供了一個win編譯的exe版本,用py2exe編譯。
linux下直接輸入文件名即可。

下載文件以及源碼請到:http://www.chinaunix.net/index.php?uid=386791&url=http://bbs.chinaunix.net/viewthread.php?tid=1256415

linux編碼爲utf-8,win編碼爲gbk

linux 代碼:

#!/usr/bin/python
#coding:utf-8

# *************************************************
# author   :   smallfish <[email protected]>
#              http://hi.baidu.com/smallfish_xy
# 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("&nbsp;", ' ', 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 郵件:[email protected]'
print '# 輸入需要翻譯的單詞 比如 >>> fish 輸入q!退出本程序'
input = raw_input('>>> ')
while input != 'q!' :
search_word(input)
input = raw_input('>>> ')

發佈了44 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章