Question
Find the text for Hamlet by William Shakespeare at Project Gutenberg EBook. Since we will be parsing the contents, be sure the url is for
Find the text for Hamlet by William Shakespeare at Project Gutenberg EBook. Since we will be parsing the contents, be sure the url is for the raw text version of the play.
use the most downloaded text version (ebook #1524)
create the function get_hamlet_url which returns the url (a string) for this play.
Create the function get_remote_text(url) that fetches the remote data using the url passed in. You can decide which library you want to use. Wrap your entire function with a try/except construct. If any error happens, return None otherwise return the result as a string.
Note. Hamlet is encoded in UTF-8 (unicode). We will learn more about that soon, but for now if you decide to use the urllib.request library you need to decode the data using the following:data = rd.read()
output = data.decode('utf-8')
Note: If you use the requests library, it does this automatically for you in most cases. You can add response.encoding = 'utf-8' in v2.py, if you are not passing the tests, before you return the text.
Step 3:
Create a function named extract_passage that has a single parameter (the text from step 2) and extracts the famous passage from Hamlet (i.e. to be or not to be) using the following restrictions:
You cannot use any hardcoded numbers (e.g. 1526). You should use the power of methods on the string type to find these indices (revisit the lesson on Strings or the Python documentation)
Use Python's slice notation (e.g. return passage[start:end] )
Extract the entire passage. The passage starts with To and ends with a period . (just before OPHELIA speaks).
You should remove any leading or trailing white space
Note: If you can't seem to fetch the contents of Hamlet, first try getting it via the browser with which you are using repl.it. Make sure to test your code before running the submission tests as well.
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