第十二章:实战项目
项目一:简单网页爬虫
爬虫就是"自动化浏览网页的机器人"。让我们用 requests + BeautifulSoup 抓取网页内容。
bash
# 先安装依赖
# pip install requests beautifulsoup4
import requests
from bs4 import BeautifulSoup
def fetch_news(url):
"""抓取网页标题和链接"""
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 "无标题"
links = soup.find_all("a", limit=10)
print(f"📄 页面标题:{title}")
print(f"🔗 前 10 个链接:")
for link in links:
href = link.get("href", "")
text = link.get_text(strip=True)[:30]
if href and text:
print(f" [{text}] → {href[:60]}")
# 使用示例(请替换为实际可访问的 URL)
fetch_news("https://example.com")bash
📄 页面标题:Example Domain
🔗 前 10 个链接:
[More information...] → https://www.iana.org/domains/example⚠️ 注意: ⚠️ 爬虫注意事项:遵守网站的robots.txt规则设置请求间隔,不要对服务器造成压力仅用于学习目的,商业用途请获取授权
项目二:文件批量重命名
bash
import os
from datetime import datetime
def batch_rename(directory, prefix="file", start_num=1):
"""批量重命名目录中的文件"""
renamed_count = 0
for filename in sorted(os.listdir(directory)):
old_path = os.path.join(directory, filename)
# 跳过子目录
if not os.path.isfile(old_path):
continue
# 获取文件扩展名
_, ext = os.path.splitext(filename)
# 生成新文件名:前缀_序号.扩展名
new_name = f"{prefix}_{start_num:04d}{ext}"
new_path = os.path.join(directory, new_name)
# 避免覆盖已有文件
if os.path.exists(new_path):
print(f" ⚠️ 跳过:{new_name} 已存在")
continue
os.rename(old_path, new_path)
print(f" ✅ {filename} → {new_name}")
renamed_count += 1
start_num += 1
return renamed_count
# 使用示例
count = batch_rename("./photos", prefix="vacation", start_num=1)
print(f"\n共重命名 {count} 个文件")bash
✅ IMG_20240101.jpg → vacation_0001.jpg
✅ IMG_20240102.jpg → vacation_0002.jpg
✅ IMG_20240103.png → vacation_0003.png
共重命名 3 个文件项目三:调用天气 API
bash
# pip install requests
import requests
def get_weather(city):
"""查询天气信息(使用 wttr.in 免费 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} 天气")
print(f" 🌡️ 温度:{temp}°C(体感 {feels_like}°C)")
print(f" 💧 湿度:{humidity}%")
print(f" 🌤️ 天气:{desc}")
print(f" 💨 风速:{wind} km/h")
except requests.RequestException as e:
print(f"❌ 获取天气失败:{e}")
# 使用示例
get_weather("Beijing")bash
🌍 Beijing 天气
🌡️ 温度:32°C(体感 35°C)
💧 湿度:45%
🌤️ 天气:Sunny
💨 风速:12 km/h