Skip to content

Instantly share code, notes, and snippets.

@wkcn
Created March 3, 2015 11:33
Show Gist options
  • Save wkcn/e2a6e4d70130cb032940 to your computer and use it in GitHub Desktop.
Save wkcn/e2a6e4d70130cb032940 to your computer and use it in GitHub Desktop.
一个简单的单词翻译
#-*- coding:utf-8 -*-
import urllib2
def GetHtml(word):
address = 'http://cn.bing.com/dict/search?&q=' + word;
try:
html = urllib2.urlopen(address).read().decode('utf-8')
return html
except:
return ''
def GetData(html):
#返回一个内容为释义的列表
head = '<span class="def"><span>'
tail = '</span></span>'
headLen = len(head)
tailLen = len(tail)
htmlLen = len(html)
result = []
for i in range(htmlLen):
if html[i:i+headLen] == head:
for j in range(i+headLen,htmlLen):
if html[j:j+tailLen] == tail:
break
#我们得到的释义在[i+head,j]
text = html[i+headLen:j]
result.append(text)
return result
def Translate(word):
html = GetHtml(word)
#html = '<span class="def"><span>a</span></span>'
data = GetData(html)
for t in data:
print t
#Translate('hello')
while True:
word = raw_input('请输入要查询的单词: ')
if word != '':
print word + '的意思是:'
Translate(word)
print '----------'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment