Python定义一个单词?

时间:2013-10-15 16:41:08

标签: python words

我可以使用python来定义这样的单词:

x=raw_input("Enter Word: ")
x=define(x)
print x

这可能吗?如果si .. 2怎么做?

这只是一个“假设”问题..所以不要讨厌PLZ。

2 个答案:

答案 0 :(得分:5)

你可以使用re和urllib2模块来解决这个问题。 试试这个代码dawg。

import re
import urllib2
def find(x):
    srch=str(x)
    x=urllib2.urlopen("http://dictionary.reference.com/browse/"+srch+"?s=t")
    x=x.read()
    items=re.findall('<meta name="description" content="'+".*$",x,re.MULTILINE)
    for x in items:
        y=x.replace('<meta name="description" content="','')
        z=y.replace(' See more."/>','')
        m=re.findall('at Dictionary.com, a free online dictionary with pronunciation,              synonyms and translation. Look it up now! "/>',z)
        if m==[]:
            if z.startswith("Get your reference question answered by Ask.com"):
                print "Word not found! :("
            else:
                print z
    else:
            print "Word not found! :("
x=raw_input("Enter word to find: ")
find(x)

让我知道它是怎么回事!

答案 1 :(得分:0)

import re
from urllib2 import urlopen


def define(word):
    html = urlopen("http://dictionary.reference.com/browse/" + word + "?s=t").read()
    items = re.findall('<div class="def-content">\s.*?</div>', html, re.S)
    defs = [re.sub('<.*?>','', x).strip() for x in items]
    for i, d in enumerate(defs):
        print '\n', '%s'%(i+1), d


define(raw_input("Enter the word you'd like to define: "))

这与@SamTubb给出的答案基本相同 如果dictionary.com没有您输入的字词的定义,那么您将获得HTTPError