2013-10-15 50 views
-2

我可以使用Python來定義這樣一句話:Python定義一個單詞?

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

這可能嗎?如果si ..怎麼做2?

這只是一個「如果」的問題..所以沒有討厭plz。

+2

我懷疑他試過什麼。這看起來更像是一個「我可以」/「假如」的問題,只是一個好奇的新手。 –

+1

是的。非常,我幾乎不知道任何東西..只是想像虛構的字典。 – DodongoWizard11

回答

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

耶!有效!感謝Sam! – DodongoWizard11

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

相關問題