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二维码生成库qrcode安装和使用示例

Python二维码生成库qrcode安装和使用示例

二维码简称 QR Code(Quick Response Code),学名为快速响应矩阵码,是二维条码的一种,由日本的 Denso Wave 公司于 1994 年发明。现随着智能手机的普...

跨平台python异步回调机制实现和使用方法

1 将下面代码拷贝到一个文件,命名为asyncore.py 复制代码 代码如下:import socketimport selectimport sys def ds_asyncore(...

Python 从相对路径下import的方法

例如我们有如下结构的文件: pkg/ __init__.py libs/ some_lib.py __init__.py components/ code.py __i...

win10环境下配置vscode python开发环境的教程详解

win10环境下配置vscode python开发环境的教程详解

前言 VScode是一个相当优秀的IDE,具备开源、跨平台、模块化、插件丰富、启动时间快、颜值高、可高度定制等等优秀的特质,不愧是微软爸爸的私生子。 所以用VScode来编写Pyth...

对python For 循环的三种遍历方式解析

实例如下所示: array = ["a","b","c"] for item in array: print(item) for index in range(len...