Simple web content download with urllib.request

suggest change

The standard library module urllib.request can be used to download web content:

from urllib.request import urlopen

response = urlopen('http://stackoverflow.com/questions?sort=votes')    
data = response.read()

# The received bytes should usually be decoded according to the response's character set
encoding = response.info().get_content_charset()
html = data.decode(encoding)

If get_content_charset() returns None, data.decode(encoding) raises TypeError. Opening that Stack Overflow URL from here returned HTTP 403, so the body was not decoded.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents