gpt4 book ai didi

python - 在 Python 中定义单词

转载 作者:可可西里 更新时间:2023-11-01 13:29:51 25 4
gpt4 key购买 nike

这看起来像是一个副本: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(函数名)
  • 用户定义输入

如果你想查看这个的 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!

最佳答案

output_word = output_word.decode()

将字节转换为字符串。


更新

这是聊天脚本的最后状态(离完美还很远...):

import requests
from lxml import html

def define_word(word):
response = requests.get(
"http://dictionary.reference.com/browse/{}?s=t".format(word))
tree = html.fromstring(response.text)
title = tree.xpath('//title/text()')
print(title)
defs = tree.xpath('//div[@class="def-content"]/text()')
# print(defs)

defs = ''.join(defs)
defs = defs.split('\n')
defs = [d for d in defs if d]
for d in defs:
print(d)

define_word('python')

关于python - 在 Python 中定义单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31694137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com