Chapter 12: Practical Projects
Project 1: Simple Web Scraper
A web scraper is a "robot that automatically browses web pages." Let's use requests + BeautifulSoup to fetch web content.
bash
# Install dependencies first
# pip install requests beautifulsoup4
import requests
from bs4 import BeautifulSoup
def fetch_news(url):
"""Fetch page titles and links"""
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
}
response = requests.get(url, headers=headers, timeout=10)
response.encoding = response.apparent_encoding
soup = BeautifulSoup(response.text, "html.parser")
title = soup.title.string if soup.title else "No Title"
links = soup.find_all("a", limit=10)
print(f"📄 Page Title: {title}")
print(f"🔗 First 10 links:")
for link in links:
href = link.get("href", "")
text = link.get_text(strip=True)[:30]
if href and text:
print(f" [{text}] → {href[:60]}")
# Usage example (replace with an actual accessible URL)
fetch_news("https://example.com")bash
📄 Page Title: Example Domain
🔗 First 10 links:
[More information...] → https://www.iana.org/domains/example⚠️ Note: ⚠️ Web scraping guidelines: Follow the website's robots.txt rulesSet request intervals to avoid putting pressure on serversUse for learning purposes only; obtain authorization for commercial use
Project 2: Batch File Renaming
bash
import os
from datetime import datetime
def batch_rename(directory, prefix="file", start_num=1):
"""Batch rename files in a directory"""
renamed_count = 0
for filename in sorted(os.listdir(directory)):
old_path = os.path.join(directory, filename)
# Skip subdirectories
if not os.path.isfile(old_path):
continue
# Get file extension
_, ext = os.path.splitext(filename)
# Generate new filename: prefix_number.ext
new_name = f"{prefix}_{start_num:04d}{ext}"
new_path = os.path.join(directory, new_name)
# Avoid overwriting existing files
if os.path.exists(new_path):
print(f" ⚠️ Skipped: {new_name} already exists")
continue
os.rename(old_path, new_path)
print(f" ✅ {filename} → {new_name}")
renamed_count += 1
start_num += 1
return renamed_count
# Usage example
count = batch_rename("./photos", prefix="vacation", start_num=1)
print(f"\nRenamed {count} files in total")bash
✅ IMG_20240101.jpg → vacation_0001.jpg
✅ IMG_20240102.jpg → vacation_0002.jpg
✅ IMG_20240103.png → vacation_0003.png
Renamed 3 files in totalProject 3: Calling a Weather API
bash
# pip install requests
import requests
def get_weather(city):
"""Query weather information (using wttr.in free API)"""
url = f"https://wttr.in/{city}?format=j1"
try:
response = requests.get(url, timeout=10)
data = response.json()
current = data["current_condition"][0]
location = data["nearest_area"][0]
city_name = location["areaName"][0]["value"]
temp = current["temp_C"]
feels_like = current["FeelsLikeC"]
humidity = current["humidity"]
desc = current["weatherDesc"][0]["value"]
wind = current["windspeedKmph"]
print(f"🌍 {city_name} Weather")
print(f" 🌡️ Temperature: {temp}°C (Feels like {feels_like}°C)")
print(f" 💧 Humidity: {humidity}%")
print(f" 🌤️ Conditions: {desc}")
print(f" 💨 Wind Speed: {wind} km/h")
except requests.RequestException as e:
print(f"❌ Failed to get weather: {e}")
# Usage example
get_weather("Beijing")bash
🌍 Beijing Weather
🌡️ Temperature: 32°C (Feels like 35°C)
💧 Humidity: 45%
🌤️ Conditions: Sunny
💨 Wind Speed: 12 km/h