๐ท Introduction Python becomes truly powerful when you start using external libraries . These are packages developed by the community that allow you to perform complex tasks without writing everything from scratch.
From making HTTP requests to analyzing data or scraping websites, external libraries are essential for real-world development.
In this lesson, you will learn:
โ How to install and manage libraries using pip โ How to use virtual environments (venv) โ How to work with popular libraries:
requests (HTTP requests)BeautifulSoup (web scraping)pandas (data manipulation basics)๐ฉ 1. Installing Libraries with pip ๐น Install a package pip install requests๐น Install a specific version pip install requests==2.31.0๐น List installed packages pip list๐น Uninstall a package pip uninstall requests๐ฉ 2. Virtual Environments (venv) A virtual environment isolates your project dependencies.
๐น Create a virtual environment python -m venv venv๐น Activate it Windows: venv\Scripts\activateLinux / Mac: source venv/bin/activate๐น Why use venv? โ Avoid version conflicts โ Keep projects independent โ Professional development practice
๐ฉ 3. Using the requests Library Used for making HTTP requests.
๐น Example: GET request import requests
response = requests.get("https://api.github.com")
print(response.status_code)
print(response.text)๐น JSON response data = response.json()
print(data)๐ฉ 4. Web Scraping with BeautifulSoup Used to extract data from HTML pages.
๐น Installation pip install beautifulsoup4๐น Example from bs4 import BeautifulSoup
import requests
html = requests.get("https://example.com").text
soup = BeautifulSoup(html, "html.parser")
print(soup.title.text)๐ฉ 5. Data Handling with Pandas Pandas is used for data analysis.
๐น Installation pip install pandas๐น Example import pandas as pd
data = {
"Name": ["Ali", "Sara"],
"Age": [22, 25]
}
df = pd.DataFrame(data)
print(df)๐น Read CSV with pandas df = pd.read_csv("data.csv")
print(df.head())
๐ฉ 6. Best Practices โ Use virtual environments โ Pin versions (requirements.txt) โ Avoid installing unnecessary packages โ Read documentation
๐ง 7. Exercises (Hidden Solutions) Exercise 1 โ Install and use requests
Show solution
import requests
r = requests.get("https://api.github.com")
print(r.status_code)
Exercise 2 โ Parse HTML title
Show solution
from bs4 import BeautifulSoup
import requests
html = requests.get("https://example.com").text
soup = BeautifulSoup(html, "html.parser")
print(soup.title.text)
Exercise 3 โ Create a pandas DataFrame
Show solution
import pandas as pd
df = pd.DataFrame({
"Name": ["Ali", "Sara"],
"Age": [22, 25]
})
print(df)
Exercise 4 โ Read CSV with pandas
Show solution
import pandas as pd
df = pd.read_csv("data.csv")
print(df.head())
Exercise 5 โ Create a virtual environment
Show solution
python -m venv myenv
source myenv/bin/activate
๐ฆ Conclusion In this lesson, you learned how to:
โ Install and manage external libraries โ Use virtual environments โ Make HTTP requests โ Scrape websites โ Work with data using pandas
These skills are essential for:
Web development Data science Automation APIs ๐ Mini Project โ Web Scraper + Data Analyzer (Books Website) ๐ท Project Idea Build a Python program that:
Scrapes book data from a website Extracts: Stores the data in a pandas DataFrame Saves it as a CSV file Performs simple analysis ๐ Target Website (safe for practice) ๐ http://books.toscrape.com/
๐งฉ Part 1 โ What You Will Learn โ Real web scraping workflow โ HTML parsing โ Data extraction โ Data cleaning โ Data analysis with pandas
๐งช Part 2 โ Install Required Libraries pip install requests beautifulsoup4 pandas๐งจ Part 3 โ Full Solution
Show solution
import requests
from bs4 import BeautifulSoup
import pandas as pd
# Step 1: Send HTTP request
url = "http://books.toscrape.com/"
response = requests.get(url)
# Step 2: Parse HTML
soup = BeautifulSoup(response.text, "html.parser")
# Step 3: Find all books
books = soup.find_all("article", class_="product_pod")
data = []
for book in books:
# Title
title = book.h3.a["title"]
# Price
price = book.find("p", class_="price_color").text
price = float(price.replace("ยฃ", ""))
# Rating (class contains rating)
rating_class = book.p["class"][1]
# Convert rating text to number
rating_map = {
"One": 1,
"Two": 2,
"Three": 3,
"Four": 4,
"Five": 5
}
rating = rating_map.get(rating_class, 0)
data.append({
"Title": title,
"Price": price,
"Rating": rating
})
# Step 4: Create DataFrame
df = pd.DataFrame(data)
# Step 5: Save to CSV
df.to_csv("books.csv", index=False)
# Step 6: Analysis
print("First 5 books:")
print(df.head())
print("\nAverage price:", df["Price"].mean())
print("Highest rated books:")
print(df[df["Rating"] == 5])
๐ Part 4 โ Explanation
Show solution
โ
Step 1 โ Request the page requests.get(url)Downloads HTML content.
โ
Step 2 โ Parse HTML BeautifulSoup(response.text, "html.parser")Converts HTML into a searchable structure.
โ
Step 3 โ Extract data soup.find_all("article", class_="product_pod")Finds all book blocks.
โ
Step 4 โ Clean data Remove currency symbol (ยฃ) Convert to float Convert rating text โ number โ
Step 5 โ Use pandas pd.DataFrame(data)Turns raw data into a structured table.
โ
Step 6 โ Save results df.to_csv("books.csv")โ
Step 7 โ Analyze data df["Price"].mean()๐ Example Output First 5 books: Title Price Rating Book A 51.77 3 Book B 53.74 1 Average price: 35.12
๐ง What You Practiced โ HTTP requests โ HTML parsing โ Data extraction โ Data cleaning โ Data analysis โ File export
๐ฏ Bonus Challenges ๐ Extend the project:
Scrape multiple pages Sort books by price Find cheapest book Filter books with rating โฅ 4 Create a simple chart (matplotlib)