''' getpagefromurl.py Jeff Ondich, 16 Jan 2009 Tianna Avery, November 2018, updated for Python 3 This program gives a brief illustration of how to use the urllib.request module to retrieve files stored on the network. The program accepts an URL as a command-line argument, retrieves the text of the file specified by that URL, and prints the text to standard output. ''' import sys import urllib.request if len(sys.argv) == 1: print(f'Usage: {sys.argv[0]} url', file=sys.stderr) exit() url = sys.argv[1] try: request = urllib.request.Request(url) response = urllib.request.urlopen(request) page_text = response.read().decode('utf-8') print(page_text) except Exception as e: print(f'Trouble retrieving {url}.', file=sys.stderr) print(f' {e}', file=sys.stderr)