# Program to print contents of a URL # It assumes the resource that the URL points to returns # UTF-8 encoded characters # # Matt Bishop, MHI 289I, Winter 2019 # import urllib.request # # ask for the URL # try: urlname = input("Please enter the URL: ") except EOFError: print("Bye!") except: print("That's not a valid string. ") else: # # got it -- now try to read from the URL # try: webpage = urllib.request.urlopen(urlname) except Exception as msg: print("URL retrieval failed!", msg) else: # Read the web page a line at a time, and prefix each line # with the line number and print it count = 1 for i in webpage.readlines(): print("%5d " % count, i) count += 1