Python define a word?

前端 未结 2 1105
无人共我
无人共我 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('<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: "))
    

    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.

    0 讨论(0)
  • 2021-01-29 12:25

    You can probobally do this using the re and urllib2 modules.. Try this code out 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)
    

    Let me know how it goes!

    0 讨论(0)
提交回复
热议问题