用python通过百度来做的英汉字典
import string
import urllib
key1 = "<img"
key2 = "/>"
pos1 = 0
pos2 = 0
word = raw_input("please input word:")
params = {}
params["word"] = word
params["ssid"] = 0
params["from"] = 0
params["bd_page_type"] = 0
params["tn"] = "wisedict"
params["mark"] = 3
params["uid"] = "frontui_1266379660_6450"
params["vit"] = "qh"
#purl = ""
url = "http://m.baidu.com/s?"
#for item in params:
# purl = purl + ("%s=%s&" % (item,str(params[item])))
url = url + urllib.urlencode(params)
#print url
s = urllib.urlopen(url).readlines()[10]
while 1:
pos1 = 0
pos2 = 0
pos1 = s.find(key1,pos2)
if pos1 < 0:
break
else:
pos2 = s.find(key2,pos1) + len(key2)
s = s[:pos1] + s[pos2:]
s = s.replace("[]","")
sarr = s.split("<br/>")
for line in sarr:
print line
import urllib
key1 = "<img"
key2 = "/>"
pos1 = 0
pos2 = 0
word = raw_input("please input word:")
params = {}
params["word"] = word
params["ssid"] = 0
params["from"] = 0
params["bd_page_type"] = 0
params["tn"] = "wisedict"
params["mark"] = 3
params["uid"] = "frontui_1266379660_6450"
params["vit"] = "qh"
#purl = ""
url = "http://m.baidu.com/s?"
#for item in params:
# purl = purl + ("%s=%s&" % (item,str(params[item])))
url = url + urllib.urlencode(params)
#print url
s = urllib.urlopen(url).readlines()[10]
while 1:
pos1 = 0
pos2 = 0
pos1 = s.find(key1,pos2)
if pos1 < 0:
break
else:
pos2 = s.find(key2,pos1) + len(key2)
s = s[:pos1] + s[pos2:]
s = s.replace("[]","")
sarr = s.split("<br/>")
for line in sarr:
print line
整个代码40几行,通过下载m.baidu.com里面的字典的wml文件,进行解析,然后输出。 细节上还不是很完善,不过基本可用了代码的逻辑很简单,先生成地址,然后通过urllib把wml文件下载回来(这个是给手机浏览的)没找到可用的wml解析库,也尝试用xml方式处理,不过效果并不好。但下载回来的wml文件里面关于单词的解释都在第十行这就好办了,接下来处理了一下,最后输出~