当前位置: 首页 > news >正文

python基础入门:8.1项目1:爬虫与数据分析

Python爬虫与数据分析全流程实战:从数据采集到可视化呈现

# 综合案例:电商价格监控分析系统
import requests
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt# 配置参数
HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ''AppleWebKit/537.36 (KHTML, like Gecko) ''Chrome/91.0.4472.124 Safari/537.36'
}def scrape_products(url):"""爬取商品信息"""products = []try:response = requests.get(url, headers=HEADERS, timeout=10)soup = BeautifulSoup(response.content, 'html.parser')items = soup.select('div.product-item')for item in items:name = item.select_one('h2.product-title').text.strip()price = item.select_one('span.price').text.strip()rating = item.select_one('div.rating').attrs['data-score']reviews = item.select_one('a.reviews-count').text.split()[0]products.append({'name': name,'price': price,'rating': float(rating),'reviews': int(reviews.replace(',', ''))})except Exception as e:print(f"爬取失败: {str(e)}")return productsdef clean_data(df):"""数据清洗处理"""# 价格处理df['price'] = df['price'].str.replace('$', '').astype(float)# 过滤异常值df = df[(df['price'] > 0) & (df['price'] < 10000)]# 分类处理df['category'] = df['name'].str.extract(r'([A-Za-z]+) Pro')df['category'] = df['category'].fillna('Other')return dfdef visualize_data(df):"""数据可视化展示"""plt.figure(figsize=(15, 8))# 价格分布直方图plt.subplot(2, 2, 1)df['price'].plot(kind='hist', bins=20, color='skyblue')plt.title('价格分布')plt.xlabel('价格 ($)')# 评分与价格散点图plt.subplot(2, 2, 2)plt.scatter(df['rating'], df['price'], alpha=0.6)plt.title('评分 vs 价格')plt.xlabel('评分')plt.ylabel('价格 ($)')# 类别销量柱状图plt.subplot(2, 2, 3)df['category'].value_counts().plot(kind='bar', color='salmon')plt.title('商品类别分布')plt.xticks(rotation=45)# 价格趋势折线图plt.subplot(2, 2, 4)df.sort_values('rating').groupby('rating')['price'].mean().plot(marker='o', color='green')plt.title('不同评分的平均价格')plt.xlabel('评分')plt.ylabel('平均价格 ($)')plt.tight_layout()plt.savefig('product_analysis.png', dpi=300)plt.show()# 主程序
if __name__ == "__main__":# 示例电商网站(需替换实际目标网站)base_url = "https://example-store.com/products?page="all_products = []for page in range(1, 6):  # 爬取前5页url = f"{base_url}{page}"print(f"正在爬取: {url}")all_products.extend(scrape_products(url))df = pd.DataFrame(all_products)df = clean_data(df)print("\n数据概览:")print(df.describe())print("\n保存数据到products.csv")df.to_csv('products.csv', index=False)visualize_data(df)
一、高效爬虫开发技巧
  1. 网页解析优化策略
# 使用CSS选择器最佳实践
def optimized_parser(html):soup = BeautifulSoup(html, 'lxml')  # 使用更快的解析器# 选择器优化技巧products = soup.select('div[data-product-id]')  # 通过属性选择for product in products:# 链式查找减少查询次数name = product.find(class_='title').get_text(strip=True)# 使用data属性获取信息price = product.find('meta', {'itemprop': 'price'})['content']# 异常处理try:rating = product.select_one('.stars').attrs['title']except (AttributeError, KeyError):rating = None
  1. 反爬虫应对方案
# 高级请求配置
session = requests.Session()
session.proxies = {'http': 'http://10.10.1.10:3128','https': 'http://10.10.1.10:1080',
}# 随机延迟
from random import uniform
from time import sleepdef safe_request(url):sleep(uniform(1, 3))  # 随机延迟1-3秒return session.get(url)# 使用代理中间件示例
class ProxyMiddleware:def process_request(self, request, spider):request.meta['proxy'] = "http://user:pass@proxy_ip:port"
二、数据清洗实战技巧
  1. 常见数据问题处理
def advanced_cleaning(df):# 处理缺失值df['rating'] = df['rating'].fillna(df['rating'].median())# 处理重复值df = df.drop_duplicates(subset=['name'], keep='last')# 处理异常值q_low = df['price'].quantile(0.01)q_high = df['price'].quantile(0.99)df = df[(df['price'] > q_low) & (df['price'] < q_high)]# 日期处理df['release_date'] = pd.to_datetime(df['release_date'], errors='coerce', format='%Y-%m')# 文本清洗df['name'] = df['name'].str.replace(r'[^\w\s]', '', regex=True)return df
  1. 数据转换技巧
# 创建价格分段
bins = [0, 50, 100, 200, 500, 1000]
labels = ['<50', '50-100', '100-200', '200-500', '500+']
df['price_range'] = pd.cut(df['price'], bins=bins, labels=labels)# 计算价格指数
df['price_index'] = (df['price'] / df.groupby('category')['price'].transform('mean')).round(2)# 时间序列转换
monthly_sales = df.resample('M', on='date')['price'].sum()
三、可视化进阶技巧
  1. 交互式可视化(使用Plotly)
import plotly.express as px# 创建交互式散点图
fig = px.scatter(df, x='rating', y='price', color='category',hover_data=['name'], title='商品分布分析')
fig.show()# 创建桑基图(Sankey Diagram)
category_flow = df.groupby(['category', 'price_range']).size().reset_index(name='count')
fig = px.sankey(category_flow, nodes={'label': list(df['category'].unique()) + labels},link=dict(source=category_flow['category'],target=category_flow['price_range'],value=category_flow['count']))
fig.show()
  1. 自动化报告生成
from pandas_profiling import ProfileReport# 生成数据分析报告
profile = ProfileReport(df, title="商品数据分析报告")
profile.to_file("product_report.html")# 使用Jupyter Notebook集成
from IPython.display import HTML
HTML(profile.to_html())

性能优化指南

  1. 使用lxml解析器替代默认的html.parser
  2. 批量处理数据时使用pandas向量化操作
  3. 避免在循环中多次访问DataFrame
  4. 使用Dask处理超大规模数据
  5. 缓存已爬取的页面内容
  6. 使用异步请求(aiohttp)提升爬虫效率
  7. 对数值型数据使用category类型节省内存
  8. 使用内存映射文件处理超大数据集
开始
发送HTTP请求
成功?
解析HTML内容
错误处理
提取数据
存储原始数据
数据清洗
数据分析
可视化呈现
生成报告
结束

项目扩展方向

  1. 增加自动化邮件报警功能
  2. 集成数据库存储(MySQL/MongoDB)
  3. 开发Web仪表盘(Flask/Django)
  4. 添加机器学习价格预测模块
  5. 实现分布式爬虫架构
  6. 构建RESTful API数据接口
  7. 开发浏览器扩展程序
  8. 制作自动化日报系统

避坑指南

  1. 遵守robots.txt协议
  2. 设置合理的请求间隔(>2秒)
  3. 处理SSL证书验证问题
  4. 注意网站内容的版权限制
  5. 使用try-except处理网络异常
  6. 定期检查选择器有效性
  7. 监控数据质量异常
  8. 做好数据备份机制
http://www.lryc.cn/news/535311.html

相关文章:

  • git 克隆指定 tag 的项目
  • DeepSeek学习笔记之——初识DeepSeek
  • Linux 调用可执行程序
  • MVCC面试怎么答
  • 用Go实现 SSE 实时推送消息(消息通知)——思悟项目技术4
  • 0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型
  • vue3:动态渲染后端返回的图片
  • DeepSeek小白初识指南
  • 图像锐化(QT)
  • 38.社区信息管理系统(基于springboothtml)
  • 游戏引擎学习第98天
  • 音频知识基础
  • 【AI赋能】蓝耘智算平台实战指南:3步构建企业级DeepSeek智能助手
  • LabVIEW无人机飞行状态监测系统
  • DeepSeek模型架构及优化内容
  • html语义化
  • python学习第十四天之机器学习名词介绍
  • 天津三石峰科技——汽车生产厂的设备振动检测项目案例
  • 汽车与AI深度融合:CES Asia 2025前瞻
  • 前端实现 GIF 图片循环播放
  • React - 事件绑定this
  • STM32系统架构介绍
  • Macbook Pro快速搭建Easysearch学习环境
  • 老游戏回顾:SWRacer
  • Firefox无法隐藏标题栏
  • vue基础(五)
  • MySQL的深度分页如何优化?
  • 深度学习每周学习总结R6(RNN实现阿尔茨海默病诊断)
  • Node.js 多模态图像描述服务 调用siliconflow:现代 JavaScript 实践
  • 机器学习数学基础:21.特征值与特征向量