get HTML page using urllib - Python web client
Keyboard shortcuts
Press ← or → to navigate between chapters
Press S or / to search in the book
Press ? to show this help
Press Esc to hide this help
get HTML page using urllib
urllib is a rather low level library. It comes standard with Python and thus it might be useful for simple tasks.
import urllib.request
# fh is like a filehandle
with urllib.request.urlopen('https://python.org/') as fh:
html = fh.read()
print(html)
This code will print the content of the HTML page download from the given web site.
If there is an error the request will raise an exception.
For example if ask for a page that does not exist on the server we’ll get a urllib.error.HTTPError with the text HTTP Error 404: Not Found.
If we ask for a URL that does not even resolve we’ll get a urllib.error.URLError exception the text Name or service not known.
We should probably wrap the code in a try - except expression.