Python define a word?

2019-09-23 15:42发布

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?

This IS just a "what if" question.. so no hate plz.

标签: python words
2条回答
再贱就再见
2楼-- · 2019-09-23 16:20
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.

查看更多
等我变得足够好
3楼-- · 2019-09-23 16:28

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!

查看更多
登录 后发表回答