Skip to main content
Practice

What is a Module?

To fetch and process data from a webpage through web scraping, you need a module that performs specific functions.

In programming, a module is a file containing code that performs certain functions, which can be imported and used by other programs.

For Python web scraping, there are several modules available, with the most notable being:

  • requests: A module for fetching webpage data through network requests.

  • BeautifulSoup: A module for processing HTML data fetched from a webpage and extracting desired information.

  • selenium: A module for controlling webpages.


Features of Modules

  1. Reusable Code: A module contains code divided into functions, variables, classes, etc., segregated by function, allowing them to be packaged in a single file for reuse whenever needed.

  2. Independence: A module operates independently and should easily integrate with other code.

  3. Namespace: In programming, a namespace refers to the space where identifiers (such as the names of variables, functions, classes, etc.) are stored. Modules have their own namespace, ensuring that function or variable names within a module do not conflict with those in other modules or existing programs.


Advantages of Using Modules

  1. Improved Readability: By separating code into modules based on functionality, the structure of the program becomes easier to understand.

  2. Enhanced Maintainability: Modularity divides code into smaller, independent units, allowing specific module issues to be addressed without affecting other modules, facilitating maintenance.

  3. Reusability: Once written, a module can be reused across different programs.


Example of Usage

In Python, modules are Python files with the .py extension, which can be imported into other Python files using the import statement.

Defining a Module
# Define the say_hello function in the my_module.py file
def say_hello(name):
return f"Hello, {name}!"
Importing a Module
# Import the my_module.py file in the same directory
import my_module

# Call the say_hello function from my_module
print(my_module.say_hello("CodeFriend"))

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.