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

yipeiwu_com6年前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中pytest收集用例规则与运行指定用例详解

前言 上篇文章相信大家已经了解了pytest在cmd下结合各种命令行参数如何运行测试用例,并输出我们想要看到的信息。那么今天会讲解一下pytest是如何收集我们写好的用例?我们又有哪些...

pandas DataFrame 交集并集补集的实现

pandas DataFrame 交集并集补集的实现

1.场景,对于colums都相同的dataframe做过滤的时候 例如: df1 = DataFrame([['a', 10, '男'], ['b', 11, '...

python模块和包的应用BASE_PATH使用解析

这篇文章主要介绍了python模块和包的应用BASE_PATH使用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 python中的...

Python生成一个迭代器的实操方法

Python生成一个迭代器的实操方法

Python怎么生成一个迭代器,对于需要处理大型数据来说,迭代器是必不可少的,这样可节省大量内存空间,更加合理操作数据。 首先我们打开编辑器,这里以Sublime text3作为示范,...

Python中的asyncio代码详解

asyncio介绍 熟悉c#的同学可能知道,在c#中可以很方便的使用 async 和 await 来实现异步编程,那么在python中应该怎么做呢,其实python也...