Description
Let's allow our browser to store web pages in a file and show their content if a user types a shortened request (for example, wikipedia instead of wikipedia.org). To do that, you need to store each page as a separate file. Your program should take one command-line argument containing a directory and put files with web pages' content.
Objectives
In this stage, your program should:
- Accept a command-line argument with a directory where you save the tabs. For example, if the argument is
dir, you need to create a folder with the namedirand save web pages' content that users input in this folder. Note that if the folder already exists,FileExistsErrorwill be raised. Hence, before creating the directory, check whether it's already been made. For this, you can use theos.access()function. If the folder exists, you don't create it again. - Check whether users entered a valid URL. It must contain at least one dot, for example,
bloomberg.com. If the URL is incorrect, the browser should output an error messageInvalid URLand wait for another URL. - For now, your program can only work with two web pages:
bloomberg.comandnytimes.com. If the URL isn't either of these two, your program should output an error message. - Print the web page's content if the URL is valid and save it to a file in the directory above. You have two predefined variables with the content of the web pages that you should keep:
bloomberg_comandnytimes_com. When writing files, you don't need to change your current working directory; point the path to it from your current one. The file's name should correspond to the name of the web page. Remove the first dot from the page name and everything after it. For example, a file for the pagebloomberg.comshould be namedbloomberg. Please, don't add the .txt (or any other) extension to the file names. - Read the next input. If this input is the string
exit, the program should stop running. If not, check if the string has the same name as any file with a web page you have already saved. If it does, print the content of this file. If it doesn't, check if the string is a new valid URL. If it is, repeat step 4. If it isn't, output an error message. Make sure that you use the correct file path when reading the file. You need to access the file through the folder you created above, for example, dir/bloomberg.
Example
The greater-than symbol followed by a space (> ) represents the user input. Note that it's not part of the input.
> python browser.py dir-for-files
> bloomberg.com
The Space Race: From Apollo 11 to Elon Musk
It's 50 years since the world was gripped by historic images
of Apollo 11, and Neil Armstrong -- the first man to walk
on the moon. It was the height of the Cold War, and the charts
were filled with David Bowie's Space Oddity, and Creedence's
Bad Moon Rising. The world is a very different place than
it was 5 decades ago. But how has the space race changed since
the summer of '69? (Source: Bloomberg)
Twitter CEO Jack Dorsey Gives Talk at Apple Headquarters
Twitter and Square Chief Executive Officer Jack Dorsey
addressed Apple Inc. employees at the iPhone maker’s headquarters
Tuesday, a signal of the strong ties between the Silicon Valley giants.
> bloomberg
The Space Race: From Apollo 11 to Elon Musk
It's 50 years since the world was gripped by historic images
of Apollo 11, and Neil Armstrong -- the first man to walk
on the moon. It was the height of the Cold War, and the charts
were filled with David Bowie's Space Oddity, and Creedence's
Bad Moon Rising. The world is a very different place than
it was 5 decades ago. But how has the space race changed since
the summer of '69? (Source: Bloomberg)
Twitter CEO Jack Dorsey Gives Talk at Apple Headquarters
Twitter and Square Chief Executive Officer Jack Dorsey
addressed Apple Inc. employees at the iPhone maker’s headquarters
Tuesday, a signal of the strong ties between the Silicon Valley giants.
> nytimes
Error: Incorrect URL
> exit