Created
February 5, 2014 09:18
-
-
Save ly0/8819888 to your computer and use it in GitHub Desktop.
a etymology dict script in CLI and results are from etymonline.com 终端下的词源在线查询脚本,支持日志
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
#coding=utf-8 | |
import urllib2 | |
import sys,os | |
from bs4 import BeautifulSoup | |
import re | |
import time | |
def etymology(key): | |
page = urllib2.urlopen("http://www.etymonline.com/index.php?term=" + key).read().decode("utf-8") | |
if 'No matching terms found.' in page: | |
return None | |
try: | |
soup = BeautifulSoup(page) | |
foo = soup.findAll('div', id='dictionary')[0] | |
ret = foo.text | |
return ret | |
except: | |
pass | |
return None | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
print 'Usage: %s [WORD]' % sys.argv[0] | |
sys.exit(1) | |
foo = etymology(sys.argv[1]) | |
if foo: | |
foo = foo.replace('\n\n','') | |
foo += '\n' | |
print foo | |
#log | |
date = '%d-%d-%d' % (time.localtime().tm_year,time.localtime().tm_mon,time.localtime().tm_mday) | |
try: | |
f = open(os.path.expanduser("~/") + 'etym.log','r+') | |
except: | |
open(os.path.expanduser("~/") + 'etym.log','w').close() | |
f = open(os.path.expanduser("~/") + 'etym.log','r+') | |
if not date in f.read(): | |
f.writelines(date + '\n') | |
f.writelines('[%s]\n%s\n\n\n' % (sys.argv[1],foo)) | |
f.close() | |
else: | |
print 'Not Found' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment