本文档涵盖了 TheAlgorithms/Python 仓库中的 Web 编程实用工具和基于 HTTP 的工具。这些实用工具出于教育目的,演示了各种 Web 抓取技术、API 集成和基于 HTTP 的数据收集方法。
有关核心算法和数据结构,请参阅 核心算法。有关机器学习实现,请参阅 机器学习算法。有关仓库基础设施和 CI/CD 系统,请参阅 仓库基础设施与 CI/CD。
Web 编程实用工具分为几个类别
| 类别 | 目的 | 关键技术 |
|---|---|---|
| 网络爬虫 | 使用各种解析技术从 HTML 页面提取数据 | beautifulsoup4、lxml、httpx |
| API 集成 | 使用 REST API 进行数据收集和外部服务 | httpx、JSON 处理 |
| 数据收集 | 用于分析和机器学习的基于 HTTP 的数据收集 | httpx、numpy |
| 验证基础设施 | GitHub API 集成,用于自动化解决方案验证 | httpx、pytest |
该仓库使用不同的解析库和技术实现了多种 Web 抓取方法。
来源:web_programming/current_stock_price.py1-49 web_programming/emails_from_url.py1-118 web_programming/covid_stats_via_xpath.py1-38
代码库展示了三种主要的 HTML 解析方法:
BeautifulSoup CSS 选择器
stock_price() 函数中用于提取雅虎财经数据data-testid 属性:web_programming/current_stock_price.py36-37XPath 解析
covid_stats() 中使用 lxml 实现,用于精确的元素选择正则表达式模式匹配
emails_from_url() 中应用,用于提取电子邮件地址[a-zA-Z0-9]+@domain:web_programming/emails_from_url.py101来源:web_programming/current_stock_price.py23-38 web_programming/covid_stats_via_xpath.py27-31 web_programming/emails_from_url.py88-111
该仓库提供了具有适当错误处理和身份验证模式的 REST API 消费的教育示例。
来源:web_programming/current_weather.py19-42 web_programming/currency_converter.py178-187 web_programming/giphy.py16-23 scripts/validate_solutions.py64-79
代码库展示了多种 API 身份验证方法
| 方法 | 实现 | 使用示例 |
|---|---|---|
| 查询参数 | params={"api_key": key} | Amdoren Currency API, Giphy API |
| 自定义标头 | headers={"Authorization": f"token {token}"} | GitHub API |
| 多个提供商 | 在不同的 API 服务之间进行回退 | 天气 API |
来源:web_programming/currency_converter.py186 scripts/validate_solutions.py66-67 web_programming/current_weather.py27-38
该仓库包含专为机器学习应用设计的基于 HTTP 的数据收集实用工具。
来源:machine_learning/linear_regression.py23-40 machine_learning/linear_regression.py91-110
collect_dataset() 函数演示了用于机器学习的 HTTP 数据收集
来源:machine_learning/linear_regression.py23-40
该仓库包含复杂的 GitHub API 集成,用于自动验证 Project Euler 解决方案。
来源:scripts/validate_solutions.py51-90 scripts/validate_solutions.py98-108 scripts/project_euler_answers.json1-10
验证系统展示了高级的 Python 模块操作
动态导入过程
convert_path_to_module() 将文件路径转换为 Python 模块: scripts/validate_solutions.py30-35importlib.util.spec_from_file_location() 进行模块创建solution() 函数: scripts/validate_solutions.py104GitHub API 集成
GITHUB_TOKEN 环境变量进行身份验证: scripts/validate_solutions.py67来源: scripts/validate_solutions.py30-35 scripts/validate_solutions.py64-79 scripts/validate_solutions.py98-108
该存储库在所有工具中都展示了一致的 HTTP 客户端配置模式。
来源: web_programming/current_stock_price.py31-32 web_programming/current_weather.py29-30 web_programming/crawl_google_results.py20-25
所有 HTTP 工具都遵循一致的模式
| 配置 | 目的 | 实现 |
|---|---|---|
| 超时 | 防止请求挂起 | 在所有 httpx.get() 调用中设置 timeout=10 |
| 用户代理 | 避免机器人检测 | 自定义标头或 fake-useragent 库 |
| 重定向 | 处理 URL 重定向 | follow_redirects=True 参数 |
| 错误处理 | 优雅的故障管理 | 使用特定异常的 Try-catch 块 |
来源: web_programming/current_stock_price.py31-32 web_programming/crawl_google_results.py22 web_programming/fetch_anime_and_play.py39-40
该存储库展示了用于不同数据格式和结构的各种高级解析技术。
JSON API 响应
HTML 文档解析
XML/HTML 与 XPath
CSV 数据处理
来源: web_programming/current_weather.py29-38 web_programming/current_stock_price.py34-37 web_programming/covid_stats_via_xpath.py27-31 machine_learning/linear_regression.py28-39