This may seem like a duplicate of this: Python define a word?
However, it is not because I am trying to implement that answer (that works for that thread's OP but not for me) into my code.
Here is my function:
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!")
Note:
>>> 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
Also, this contains a bit of HTML :/ sorry but thats what the other answer used.
So, the Error when I try to use this:
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
Note: line 104 of functions.py is:
items=re.findall('<meta name="description" content="'+".*$",output_word,re.MULTILINE)
Line 210 of re.py is the last line of this function:
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
If there is anything unclear in this, please tell me (and I'm not quiet sure about what tags to add for this one :/). And thank you in advance :) Feel free to change anything or even re-writing the entire thing but just make sure to use the variables/lists:
If you wish to see the git for this, go to this link: https://github.com/DarkLeviathanz/Multitool.git
Adding:
output_word = output_word.decode()
or changing
output_word = output_word.read().decode('iso-8859-2')
has given this when this is entered: define test:
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()
will convert the bytes to a string.
UPDATE
this is the last state from the script in the chat (still far from perfect...):
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With