In Python 3, the urlopen
function returns an HTTPResponse
object, which acts like a binary file. So, when you do this:
for word in urlopen(WORD_URL).readlines():
WORDS.append(word.strip())
… you end up with a bunch of bytes
objects instead of str
objects. So when you do this:
result = result.replace("###", word, 1)
… you end up trying to replace the string "###"
within the string result
with a bytes
object, instead of a str
. Hence the error:
TypeError: Can't convert 'bytes' object to str implicitly
The answer is to explicitly decode the words as soon as you get them. To do that, you have to figure out the right encoding from the HTTP headers. How do you do that?
In this case, I read the headers, I can tell that it's ASCII, and it's obviously a static page, so:
for word in urlopen(WORD_URL).readlines():
WORDS.append(word.strip().decode('ascii'))
But in real life, you usually need to write code that reads the headers and dynamically figures it out. Or, better, install a higher-level library like requests
, which does that for you automatically.