【发布时间】:2015-07-29 07:38:40
【问题描述】:
这似乎与此重复:Python define a word?
但是,这并不是因为我试图在我的代码中实现该答案(适用于该线程的 OP,但不适用于我)。
这是我的功能:
def define_word(user_define_input):
srch = str(user_define_input[1])
output_word=urllib.request.urlopen("http://dictionary.reference.com/browse/"+srch+"?s=t")
output_word=output_word.read()
items=re.findall('<meta name="description" content="'+".*$",output_word,re.MULTILINE)
for output_word in items:
y=output_word.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!")
注意:
>>> print (user_define_input) #to show what is in the list
>>> define <word entered> #prints out the list, in this case, the program ignores user_define_input[0] and looks for [1] which is the targeted word
此外,这包含一些 HTML :/ 抱歉,但这就是其他答案使用的内容。
所以,当我尝试使用这个时的错误:
File "/Users/******/GitHub/Multitool/functions.py", line 104, in define_word
items=re.findall('<meta name="description" content="'+".*$",output_word,re.MULTILINE)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/re.py", line 210, in findall
return _compile(pattern, flags).findall(string)
TypeError: can't use a string pattern on a bytes-like object
注意: functions.py 的第 104 行是:
items=re.findall('<meta name="description" content="'+".*$",output_word,re.MULTILINE)
re.py的第210行是这个函数的最后一行:
def findall(pattern, string, flags=0):
"""Return a list of all non-overlapping matches in the string.
If one or more capturing groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern
has more than one group.
Empty matches are included in the result."""
return _compile(pattern, flags).findall(string) #line 210
如果有什么不清楚的地方,请告诉我(我不确定要为这个添加什么标签:/)。并提前感谢您:) 随意更改任何内容,甚至重写整个内容,但请确保使用变量/列表:
- define_word(用于函数名称)
- user_define_input
如果您想查看 git,请转到此链接:https://github.com/DarkLeviathanz/Multitool.git
添加:
output_word = output_word.decode()
或改变
output_word = output_word.read().decode('iso-8859-2')
在输入时给出了这个:定义测试:
Test definition, the means by which the presence, quality, or genuineness of anything is determined; a means of trial.<meta property="og:url" content="http://dictionary.reference.com/browse/test"/><link rel="shortcut icon" href="http://static.sfdict.com/dictcloud/favicon.ico"/><!--[if lt IE 9]><link rel="respond-proxy" id="respond-proxy" href="http://static.sfdict.com/app/respondProxy-d7e5f.html" /><![endif]--><!--[if lt IE 9]><link rel="respond-redirect" id="respond-redirect" href="http://dictionary.reference.com/img/respond.proxy.gif" /><![endif]--><link rel="search" type="application/opensearchdescription+xml" href="http://dictionary.reference.com/opensearch_desc.xml" title="Dictionary.com"/><link rel="publisher" href="https://plus.google.com/117428481782081853923"/><link rel="canonical" href="http://dictionary.reference.com/browse/test"/><link rel="stylesheet" href="http://dictionary.reference.com/drc/css/bootstrap.min-93899.css" type="text/css" media="all"/><link rel="stylesheet" href="http://dictionary.reference.com/drc/css/combinedSerp-8c61a.css" type="text/css" media="all"/><script type="text/javascript">var searchURL="http://dictionary.reference.com/browse/%40%40queryText%40%40?s=t";var CTSParams={"infix":"","clkpage":"dic","clksite":"dict","clkld":0};</script>
Word not found!
【问题讨论】:
标签: python html function python-3.x dictionary