Question
#-----Imported Functions---------------------------------------------# # # Below are various import statements for helpful functions. You # should be able to complete this assignment using these # functions
#-----Imported Functions---------------------------------------------# # # Below are various import statements for helpful functions. You # should be able to complete this assignment using these # functions only. Note that not all of these functions are # needed to successfully complete this assignment. YOU MAY NOT USE # ANY NON-STANDARD MODULES SUCH AS 'Beautiful Soup' OR 'Pillow'. ONLY # MODULES THAT COME WITH A STANDARD PYTHON 3 INSTALLATION MAY BE # USED. # The function for opening a web document given its URL. # (You WILL need to use this function in your solution, # either directly or via our "download" function.) from urllib.request import urlopen # Import the standard Tkinter functions. (You WILL need to use # these functions in your solution.) from tkinter import * # Functions for finding all occurrences of a pattern # defined via a regular expression, as well as # the "multiline" and "dotall" flags. (You do NOT need to # use these functions in your solution, because the problem # can be solved with the string "find" function, but it will # be difficult to produce a concise and robust solution # without using regular expressions.) from re import findall, finditer, MULTILINE, DOTALL # Import the standard SQLite functions (just in case they're # needed). from sqlite3 import * # #--------------------------------------------------------------------# #-----Downloader Function--------------------------------------------# # # This is our function for downloading a web page's content and both # saving it on a local file and returning its source code # as a Unicode string. The function tries to produce a # meaningful error message if the attempt fails. WARNING: This # function will silently overwrite the target file if it # already exists! NB: You should change the filename extension to # "xhtml" when downloading an XML document. (You do NOT need to use # this function in your solution if you choose to call "urlopen" # directly, but it is provided for your convenience.) # def download(url = 'http://www.wikipedia.org/', target_filename = 'download', filename_extension = 'html'): # Import an exception raised when a web server denies access # to a document from urllib.error import HTTPError # Open the web document for reading try: web_page = urlopen(url) except ValueError: raise Exception("Download error - Cannot find document at URL '" + url + "'") except HTTPError: raise Exception("Download error - Access denied to document at URL '" + url + "'") except: raise Exception("Download error - Something went wrong when trying to download " + \ "the document at URL '" + url + "'") # Read its contents as a Unicode string try: web_page_contents = web_page.read().decode('UTF-8') except UnicodeDecodeError: raise Exception("Download error - Unable to decode document at URL '" + \ url + "' as Unicode text") # Write the contents to a local text file as Unicode # characters (overwriting the file if it # already exists!) try: text_file = open(target_filename + '.' + filename_extension, 'w', encoding = 'UTF-8') text_file.write(web_page_contents) text_file.close() except: raise Exception("Download error - Unable to write to file '" + \ target_file + "'") # Return the downloaded document to the caller return web_page_contents # #--------------------------------------------------------------------# #-----Student's Solution---------------------------------------------# # # Put your solution at the end of this file.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started