The provided code is a web scraper that extracts the text content from a given URL and gives the user the option to order the words either by frequency or alphabetically. The program uses libraries such as requests and BeautifulSoup to fetch and parse the HTML content.
The script follows these steps to count and sort the words:
- The count_and_order_words function retrieves the text content from a given URL, cleans it, and splits it into individual words.
- If the sort_by_frequency parameter is True, the function counts the occurrences of each word and returns a list of words sorted by their frequency.
- If sort_by_frequency is False, the function returns a list of words sorted alphabetically.
- The print_ordered_words function takes a list of words and prints them, along with their counts if available.
- The user can choose to order the words by frequency (option 1), order them alphabetically (option 2), or quit the program (option 3).
- Depending on the user's choice, the count_and_order_words function is called with the appropriate arguments, and the print_ordered_words function is used to display the ordered words.
- If an invalid choice is entered, an error message is displayed.
- The program continues running until the user chooses to quit.
Make sure you have the following prerequisites installed before running the script:
- Python 3 (https://www.python.org/downloads/)
- requests library (can be installed via
pip install requests
) - BeautifulSoup library (can be installed via
pip install beautifulsoup4
) - bs4 library (can be installed via
pip install bs4
)
-
Clone this repository or download the script (
main.py
) to your local machine. -
Open a terminal or command prompt and navigate to the directory where the script is located.
-
Run the script using the following command:
python main.py