Python define a word?

前端 未结 2 1106
无人共我
无人共我 2021-01-29 11:51

Can I use python to define a word like this:

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

Is this possible? If si.. how 2 do it?

T

2条回答
  •  无人及你
    2021-01-29 12:17

    import re
    from urllib2 import urlopen
    
    
    def define(word):
        html = urlopen("http://dictionary.reference.com/browse/" + word + "?s=t").read()
        items = re.findall('
    \s.*?
    ', 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: "))

    This is essentially the same idea as the answer given by @SamTubb.
    If the dictionary.com does not have a definition for the word you enter, you'll get a HTTPError.

提交回复
热议问题