How can I print a webpage line by line in Python 3.x
<p>All I want to do is print the HTML text of a simple website. When I try printing, I get the text below in raw format with newline characters (<code>\n</code>) instead of actual new lines.</p>

<p><strong>This is my code:</strong></p>

<pre><code>import urllib.request

page = urllib.request.urlopen('http://www.york.ac.uk/teaching/cws/wws/webpage1.html', data = None)
pageText = page.read()
pageLines = page.readlines()
print(pageLines)
print(pageText)
</code></pre>

<p>I've tried all kinds of other stuff and discovered some stuff. When I try to index the <code>pageText</code> variable, even after converting it to a string, it does not find any <code>\n</code> character. If I try copying the raw text myself with the new lines represented as <code>\n</code> and I <code>print()</code> that, it converts the <code>\n</code> characters into actual new lines which is what I want. The problem is that I can't get that result without copying it myself.</p>

<p>To show you what I mean, here are some HTML snippets:</p>

<p><strong>Raw text:</strong></p>

<pre><code>b'&lt;HMTL&gt;\n&lt;HEAD&gt;\n&lt;TITLE&gt;webpage1&lt;/TITLE&gt;\n&lt;/HEAD&gt;\n&lt;BODY BGCOLOR="FFFFFf" LINK="006666" ALINK="8B4513" VLINK="006666"&gt;\n
</code></pre>

<p><strong>What I want:</strong></p>

<pre><code>b'&lt;HMTL&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;webpage1&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY BGCOLOR='FFFFFf' LINK='006666' ALINK='8B4513' VLINK='006666'&gt;
</code></pre>

<p>I also used:</p>

<pre><code>page = str(page)
lines = page.split('\n')
</code></pre>

<p>and it suprisingly did nothing.
It just printed it as one line.</p>

<p>Please, help me. I am surprised that I found nothing that worked for me. Even on forums, nothing worked.</p>

<p>Your byte string appears to have hard-coded <code>\n</code> in it. </p>

<p>For example, can't split on the value initially. </p>

<pre><code>In [1]: s = b'&lt;HMTL&gt;\n&lt;HEAD&gt;\n'

In [2]: s.split('\n')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-2-e85dffa8b351&gt; in &lt;module&gt;()
----&gt; 1 s.split('\n')

TypeError: a bytes-like object is required, not 'str'
</code></pre>

<p>So, you <code>str()</code> it, but that doesn't seem to work either. </p>

<pre><code>In [3]: str(s).split('\n')
Out[3]: ["b'&lt;HMTL&gt;\\n&lt;HEAD&gt;\\n'"]
</code></pre>

<p>But, if you <em>escape</em> the new lines it does somewhat work. </p>

<pre><code>In [4]: str(s).split('\\n')
Out[4]: ["b'&lt;HMTL&gt;", '&lt;HEAD&gt;', "'"]
</code></pre>

<p>You <em>could</em> use a raw string to split on    </p>

<pre><code>In [5]: for line in str(s).split(r'\n'):
   ...:     print(line)
   ...:
b'&lt;HMTL&gt;
&lt;HEAD&gt;
'
</code></pre>

<p>Or, if you don't want the leading <code>b</code>, you can <code>decode</code> the byte string into a string object you can then split on. </p>

<pre><code>In [9]: for line in s.decode("UTF-8").split('\n'):
   ...:     print(line)
   ...:
&lt;HMTL&gt;
&lt;HEAD&gt;
</code></pre>

python python-3.x web