Reputation: 71
I am trying to display a webpage, but since it does not view the object as a string, it does not properly display newlines (it displays \n
). How can I make a result a string properly as this does not seem to be working. Thanks!
result = urllib.request.urlopen(requesturl).read()
return str(result)
Upvotes: 5
Views: 15821
Reputation: 1411
As explained in this post, you need to handle the character encoding of the response you have from the Content-Type. urllib provides a handy way to get at this from the headers using the get_content_charset method.
from urllib.request import urlopen
with urlopen(request_url) as response:
html_response = response.read()
encoding = response.headers.get_content_charset('utf-8')
decoded_html = html_response.decode(encoding)
return decoded_html
Upvotes: 4