Python定义一个词?
我可以用python来定义这样一个词吗:Python定义一个词?,python,words,Python,Words,我可以用python来定义这样一个词吗: x=raw_input("Enter Word: ") x=define(x) print x 这可能吗?如果是。。怎么做 这只是一个“如果”的问题。。因此,没有仇恨plz。您可能可以使用re和urllib2模块来实现这一点。。 试试这个代码,老兄 import re import urllib2 def find(x): srch=str(x) x=urllib2.urlopen("http://dictionary.referenc
x=raw_input("Enter Word: ")
x=define(x)
print x
这可能吗?如果是。。怎么做
这只是一个“如果”的问题。。因此,没有仇恨plz。您可能可以使用re和urllib2模块来实现这一点。。 试试这个代码,老兄
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)
重新导入
导入urllib2
def查找(x):
srch=str(x)
x=urllib2.urlopen(“http://dictionary.reference.com/browse/“+srch+”?s=t”)
x=x.read()
项目=re.findall(“”,z)
如果m=[]:
如果z.startswith(“通过Ask.com获得你的参考问题的答案”):
打印“找不到Word:(”
其他:
打印z
其他:
打印“找不到Word:(”
x=原始输入(“输入要查找的单词:”)
查找(x)
让我知道进展如何!重新导入
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: "))
从urllib2导入urlopen
定义定义(word):
html=urlopen(“http://dictionary.reference.com/browse/“+word+”?s=t”)。读取()
items=re.findall('\s.*?',html,re.s)
defs=[re.sub(“”,,,x).strip()表示项目中的x]
对于枚举中的i,d(defs):
打印'\n','%s'(i+1),d
定义(原始输入(“输入要定义的单词:”))
这与@SamTubb给出的答案基本相同。如果dictionary.com没有你输入的单词的定义,你会得到一个
HTTPError
我怀疑他尝试了什么。这看起来更像是一个“我能不能”/“如果”的问题,只是一个好奇的新手。是的,我几乎什么都不知道。只是想做一个像虚拟字典一样的。