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网络编程详解

1、服务器就是一系列硬件或软件,为一个或多个客户端(服务的用户)提供所需的“服务”。它存在唯一目的就是等待客户端的请求,并响应它们(提供服务),然后等待更多请求。 2、客户端/服务器架...

对Python 窗体(tkinter)文本编辑器(Text)详解

如下所示: import tkinter win=tkinter.Tk() text=tkinter.Text(win) #文本编辑器(用于展示数据) text.insert(t...

python中map的基本用法示例

python中map的基本用法示例

map()函数 map() 会根据提供的函数对指定序列做映射,是内置函数 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 functi...

详细解析Python当中的数据类型和变量

详细解析Python当中的数据类型和变量

数据类型 计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值。但是,计算机能处理的远不止数值,还可以处理文本、图形、音频、视频、网页等各种各样的数据,不同...

python3 线性回归验证方法

如下所示: #-*- coding: utf-8 -*- import pandas as pd import numpy as np from patsy.highlevel im...