urllib read() changing attributes
<p>I have a basic script, which is requesting websites to get the html source code.
while crawling several websites I figured out that different attributes in the source code are being represented wrong.</p>

<p>Example:</p>

<pre><code>from urllib import request

opener = request.build_opener()
with opener.open("https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2") as response:
    html = response.read()
print(html)
</code></pre>

<p>I compared the results (<code>html</code> var) with the source code being represented by Chrome and Firefox.</p>

<p>I saw differences like these:</p>

<pre><code>Browser                        Urllib

href='rfc2616.html'            href=\'rfc2616.html\'
rev='Section'                  rev=\'Section\'
rel='xref'                     rel=\'xref\'
id='sec4.5'                    id=\'sec4.4\'
</code></pre>

<p>It looks like <code>urllib</code> is putting backslashes here to escape code.</p>

<p>Is this a bug deep inside <code>urllib</code> or is there any way to fix this problem?</p>

<p>Thanks in advance.</p>

<p><code>responce.read()</code> will return a <code>bytes</code> object, when printed its escape sequences don't get interpreted, see:</p>

<pre><code>print(b'hello\nworld') # prints b'hello\nworld'
</code></pre>

<p>You'll need to <code>decode</code> it to <code>str</code> which, when printed, evaluates the escapes correctly:</p>

<pre><code>print(html.decode())
</code></pre>

python python-3.x urllib