Skip to main content

负责工具

Project description

pylibz

一个强大的Python工具库,包含多种实用功能。

📊 AI模型定价爬虫

使用pyppeteer爬取动态网页的AI模型定价信息,并生成交互式可视化分析。

🚀 功能特性

  • 动态网页爬取: 使用pyppeteer爬取JavaScript渲染的动态网页
  • 智能数据解析: 自动识别表格结构,提取AI模型定价信息
  • 交互式可视化: 使用Plotly生成美观的交互式图表
  • 多种图表类型: 包括柱状图、对比图、综合仪表板等
  • 数据导出: 支持CSV格式数据导出
  • 中文支持: 完全支持中文显示

📦 安装依赖

# 使用uv管理依赖(推荐)
uv add pyppeteer pandas plotly

# 或使用pip
pip install pyppeteer pandas plotly

🔧 使用方法

基础用法

import asyncio
from pylibz.pricing_scraper import PricingScraper

async def main():
    # 创建爬虫实例
    scraper = PricingScraper()
    
    # 爬取编写AI的定价页面
    url = "https://api.bianxie.ai/pricing"
    df = await scraper.run(url)
    
    if df is not None:
        print(f"成功获取 {len(df)} 个AI模型的定价信息")

# 运行爬虫
asyncio.run(main())

命令行运行

# 直接运行脚本
python pylibz/pricing_scraper.py

📈 生成的文件

运行爬虫后会生成以下文件:

数据文件

  • pylibz/pricing_data.csv - 原始定价数据(CSV格式)

可视化文件

  • pylibz/pricing_chart.html - 基础价格对比图
  • pylibz/pricing_comparison.html - 提示vs补全价格对比图
  • pylibz/pricing_dashboard.html - 综合分析仪表板

🎨 可视化特性

1. 基础价格对比图

  • 分别显示提示价格和补全价格的柱状图
  • 支持悬浮显示详细信息
  • 仅显示前20个模型以保持可读性

2. 价格对比图

  • 并排显示提示价格和补全价格
  • 便于直观比较两种价格类型
  • 支持交互式缩放和筛选

3. 综合分析仪表板

  • 价格分布统计: 直方图显示价格分布情况
  • 模型类型分布: 饼图显示不同AI厂商的占比
  • 价格趋势: 散点图显示价格变化趋势
  • 平均价格对比: 整体平均价格对比

📊 数据结构

爬取的数据包含以下字段:

model_name,prompt_price,completion_price,availability,billing_type,price_text,avg_price
gpt-3.5-turbo,0.5,1.5,可用,按量计费,"提示 $0.5 / 1M tokens 补全 $1.5 / 1M tokens",1.0
  • model_name: AI模型名称
  • prompt_price: 提示价格(USD/1M tokens)
  • completion_price: 补全价格(USD/1M tokens)
  • availability: 可用性状态
  • billing_type: 计费类型
  • price_text: 原始价格文本
  • avg_price: 平均价格

🛠️ 自定义配置

修改爬取目标

# 创建爬虫实例
scraper = PricingScraper()

# 爬取其他网站
custom_url = "your-target-website.com/pricing"
df = await scraper.run(custom_url)

调整可视化参数

# 修改显示的模型数量
df_custom = df.head(10)  # 只显示前10个模型
scraper.create_bar_chart(df_custom)

自定义图表样式

可以在create_bar_chartcreate_comparison_chart等方法中修改:

  • 颜色主题
  • 图表尺寸
  • 字体样式
  • 交互功能

⚠️ 注意事项

  1. 首次运行: pyppeteer首次运行时会自动下载Chromium浏览器
  2. 网络连接: 需要稳定的网络连接访问目标网站
  3. 反爬虫: 某些网站可能有反爬虫机制,建议适当增加延时
  4. 浏览器资源: 爬虫会启动浏览器实例,确保有足够的系统资源

🐛 故障排除

常见问题

  1. 中文显示问题: 已使用Plotly解决,支持完美的中文显示
  2. 浏览器启动失败: 检查系统是否支持headless Chrome
  3. 网络超时: 增加timeout参数值
  4. 数据提取失败: 检查目标网站结构是否变化

错误处理

脚本内置了完善的错误处理机制:

  • 网络连接失败时使用示例数据
  • 浏览器异常时自动清理资源
  • 数据解析失败时提供友好提示

📄 许可证

MIT License

🤝 贡献

欢迎提交Issue和Pull Request来改进这个工具!

📞 联系方式

如有问题或建议,请通过以下方式联系:

  • 创建GitHub Issue
  • 发送邮件或其他联系方式

🔗 相关链接

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

toolsz-1.0.1.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

toolsz-1.0.1-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file toolsz-1.0.1.tar.gz.

File metadata

  • Download URL: toolsz-1.0.1.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.11

File hashes

Hashes for toolsz-1.0.1.tar.gz
Algorithm Hash digest
SHA256 59eae54c95ab218ebde8123838f53ae8c986605d92210c65dad1ad8e2cc487f6
MD5 f456428ab3450f3c12a0d9e5a8a1dd96
BLAKE2b-256 38e5d1b2b134382f8541baeb338731abbf291b6078e1c60113a89715a3aafa83

See more details on using hashes here.

File details

Details for the file toolsz-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: toolsz-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.11

File hashes

Hashes for toolsz-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 380d8e886b016564d15594b4610553ccdca5d877f28d5da6f0a8a28268f5caf5
MD5 5db3e44fc4be609db9c35d22af625a6a
BLAKE2b-256 5e619b80270096392a52410585493e3595d9f0a9efd43e55842826303f713bfc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page