Python基于有道实现英汉字典功能

yipeiwu_com5年前Python基础

本文实例讲述了Python基于有道实现英汉字典功能的方法。分享给大家供大家参考。具体如下:

import re,urllib
aa="http://dict.youdao.com/search?tab=chn&keyfrom=dict.top&q="
print ("input q! to exit ")
while 1:
  word=raw_input(">>>")
  if word=="q!":
    exit()
  else:
    word=word.replace(' ','+')
    url=aa+word
    s=urllib.urlopen(url).read()
    comm=re.compile(r'<td class="dttitle2"><font color="#013694"><b>(.*?)<\/b><\/font><\/td>')
    tem=comm.findall(s)
    com=re.compile('<td class="attributem1web">(.*?)</td>',re.S|re.M|re.I)
    result=com.findall(s)
  if tem:
    for i in tem:
      temp=i.decode('utf8').encode('cp936')
      print (temp)
      print '\n'
  else:
    print ("no such word\n")

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python多线程实现同步的四种方式

临界资源即那些一次只能被一个线程访问的资源,典型例子就是打印机,它一次只能被一个程序用来执行打印功能,因为不能多个线程同时操作,而访问这部分资源的代码通常称之为临界区。 锁机制 thre...

详解Python实现按任意键继续/退出的功能

前言 要实现该功能,需要的就是暂停程序、等待并捕捉用户的一个键盘输入,然后继续执行。Python 有内建的库能帮我们实现该功能,不过要区别对待 Windows 和 Linux。 msvc...

基于Django filter中用contains和icontains的区别(详解)

qs.filter(name__contains="e") qs.filter(name__icontains="e") 对应sql 'contains': 'LIKE BI...

Python使用一行代码获取上个月是几月

Python使用一行代码获取上个月是几月

本文介绍的关于Python时间日期处理,日期时间处理在实际应用场景中无处不在,所以这也成了编程语言中必不可少的模块,Python 也不例外。但是,你知道在Python中有多少个相关的模块...

解决python3中cv2读取中文路径的问题

如下所示: python3: img_path =  ' ' im = cv2.imdecode(np.fromfile(img_path,dtype = np.uin...