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程序设计有所帮助。

相关文章

pandas.DataFrame删除/选取含有特定数值的行或列实例

pandas.DataFrame删除/选取含有特定数值的行或列实例

1.删除/选取某列含有特殊数值的行 import pandas as pd import numpy as np a=np.array([[1,2,3],[4,5,6],[7,8...

python pandas消除空值和空格以及 Nan数据替换方法

在人工采集数据时,经常有可能把空值和空格混在一起,一般也注意不到在本来为空的单元格里加入了空格。这就给做数据处理的人带来了麻烦,因为空值和空格都是代表的无数据,而pandas中Serie...

Python常用算法学习基础教程

Python常用算法学习基础教程

本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的清晰指令,算法代表着用系统的方法描...

python通过tcp发送xml报文的方法

如下所示: # -*- coding: utf-8 -*- import socket # 使用tcp发送请求报文 def tcpsend(ip, port, xmlbw): ad...

查看Python依赖包及其版本号信息的方法

查看Python依赖包及其版本号信息的方法

查看依赖包及对应的版本号信息的方法有两种: 方法1:pip list 方法2:pip freeze 这两个同时适用于Windows和Linux系统 当pip版本过低时,会出现list命...