-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiciba.py
executable file
·37 lines (33 loc) · 1.07 KB
/
iciba.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/python
import sys
import os.path
import urllib2
import xml.etree.ElementTree as ET
def usage():
print 'Usage: %s [word]' % os.path.basename(sys.argv[0])
def main(word):
try:
ans = urllib2.urlopen('http://dict-co.iciba.com/api/dictionary.php?w=%s' % word)
root = ET.fromstring(ans.read())
print '[ %s ]' % root.find('key').text.strip()
print '------------'
plist = root.findall('pos')
alist = root.findall('acceptation')
if plist != None and alist != None:
for i in range(0, len(plist)):
if plist[i].text != None:
print plist[i].text.strip(),
if alist[i].text != None:
print alist[i].text.strip()
for j in root.findall('sent'):
print '------------'
print j.find('orig').text.strip()
print j.find('trans').text.strip()
print '------------'
except Exception, e:
print e
if __name__ == '__main__':
if len(sys.argv) < 2:
usage()
else:
main(sys.argv[1])