- 论坛徽章:
- 0
|
从dict.cn取词。d.py
- #! /usr/bin/python
- # -*- coding: utf-8 -*-
- import urllib,re,sys
- def unquote(q):
- '''在unicode终端下可以显示音标。'''
- return re.sub('&#(\d{1,5});',lambda m:unichr(int(m.group(1))),q).encode('utf8')
- if __name__=="__main__":
- if len(sys.argv)==1:
- print "请输入单词"
- sys.exit(0)
- else:
- word=sys.argv[1]
- response = urllib.urlopen("http://www.dict.cn/"+word+".htm")
- result=response.read()
- find=re.findall(r'您要查找的是不是',result,re.S)
- if len(find)>=1:
- print "没找到"
- sys.exit(0)
-
- list=re.findall(r"<table><tr>(.+?)添加生词",result,re.S)
- pronounce=re.findall(r'<span class="pronounce">(.+?)</span>',list[0])
- jies=re.findall(r'<div class="mut_jies"><strong>(.+?)</strong></div>',list[0])
- ol=re.findall(r'<ol class="mut_ol">(.+?)</ol>',list[0],re.S)
- if len(ol)>=1:
- example=re.findall(r'<li>(.+?)</li>',ol[0],re.S)
- print word,
- if len(pronounce)>=1:
- print unquote(pronounce[0])
- print re.sub('<br />','\n',jies[0])
- print
- if len(ol)>=1:
- for i in range(len(example)):
- print str(i+1)+'.',re.sub('<.+?>','',example[i]),
-
复制代码
[ 本帖最后由 two 于 2009-8-21 11:42 编辑 ] |
评分
-
查看全部评分
|