Python 英汉-汉英 词典脚本,可以安装到Linux命令中
声明:脚本文件为原作者所有,我只是做了些自定义修改而已,脚本使用有道词典网页版作为查询API
将脚本扔到/usr/bin/下面,使用命令yd + 单词进行查询. 支持英汉查询和汉英查询.
#! /usr/bin/python2.7
from bs4 import BeautifulSoup
import httplib2
import string
import socket
import time
import sys
t0 = time.time()
api = 'http://dict.youdao.com/search?q='
cache = '/home/abram/code/cache'
reload(sys)
sys.setdefaultencoding("utf-8")
def isChinese(s):
for ch in s.decode('utf-8'):
if u'\u4e00' <= ch <= u'\u9fff':
return True
return False
def getWord():
try:
word = sys.argv[1].replace('_', ' ')
except:
print("No word found.")
exit(1)
if isChinese(word):
wordf = (word,1) # 1 for Chinese; 2 for English
else: # wordf means word with a flag
wordf = (word,2)
return wordf
def getPage(wordf):
url = api + wordf[0]
h = httplib2.Http(cache)
try:
headers, content = h.request(url,"GET")
except socket.timeout:
print("Timeout, dude.")
exit(2)
if headers.status == 200:
page = content.decode('utf-8')
else:
print("Unable to retrive the page, error code:", headers.status)
exit(1)
return (page, wordf[1]) # page and the flag of the word
def getResult(pagef):
page = pagef[0]
flag = pagef[1]
soup = BeautifulSoup(page,"html.parser")
con = soup.find("div", class_="trans-container")
if flag == 1:
try:
for i in con.ul.find_all('p'):
for j in i.find_all('span'):
for k in j.find_all('a'):
print(k.string)
except:
pass
else:
try:
pronounce=soup.find("div",class_="baav").find_all("span")
print 'en '+pronounce[1].string+' us '+pronounce[3].string
except:
pass
try:
for i in con.ul.find_all('li'):
print(i.string)
except AttributeError:
print("Check your spelling and try again.")
exit(1)
try:
additional=soup.find("p",class_="additional")
print (additional.string.replace(" ","").replace("\n"," "))
except:
pass
def main():
getResult(getPage(getWord()))
t1 = time.time()
print " "
print('[finished in {:.3}s]'.format(t1 - t0))
if __name__ == '__main__':
main()

浙公网安备 33010602011771号