forked from gaopu/Python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dict.py
executable file
·28 lines (23 loc) · 906 Bytes
/
Dict.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
#!/usr/bin/python
#coding:utf-8
import urllib
import sys
import re
if len(sys.argv) == 1: #没有单词就提示用法
print "用法:./Dict.py 要查找的单词"
sys.exit()
word = ""
for x in range(len(sys.argv) - 1): #查找的可能是短语,中间有空格,如"join in",这里拼接单词
word += " " + sys.argv[x + 1]
print "单词:" + word
searchUrl = "https://dict.youdao.com/search?q=" + word + "&keyfrom=dict.index" #查找的地址
response = urllib.urlopen(searchUrl).read() #获得查找到的网页源码
#从网页源码提取出单词释义那一部分
searchSuccess = re.search(r"(?s)<div class=\"trans-container\">\s*<ul>.*?</div>",response)
if searchSuccess:
means = re.findall(r"(?m)<li>(.*?)</li>",searchSuccess.group()) #获取我们想提取的核心单词释义
print "释义:"
for mean in means:
print "\t" + mean #输出释义
else:
print "未查找到释义."