A correct version of you code – the problem was that you were having a bytes
object instead of the str
you probably expected.
I added a .decode('utf-8')
to the content read from the website, and now the html is an str
object.
You recieved the error because you were working on a bytes
object (the word_chocie
), as str
.
Note that the b
prefix you had in word_chocie
(b'hello'
) is python’s mark that this object is of type bytes
.
import random
import urllib.request
with urllib.request.urlopen('https://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain') as response:
html = response.read().decode('utf-8')
word_list = html.split()
word_chocie = random.choice(word_list)
print(word_chocie)
1
solved Trying to select a random word from a website Python 3.6