Smart Web Scraper
Extract structured data from web pages into clean JSON or CSV.
Quick Start
CODEBLOCK0
Commands
| Command | Args | Description |
|---|
| INLINECODE0 | INLINECODE1 | Extract content, optionally filtered by CSS selector |
| INLINECODE2 |
<url> [-f format] [-o file] | Auto-detect and extract all HTML tables |
|
links |
<url> [--external] [--internal] | Extract all links (href + text) |
|
structure |
<url> | Extract page structure: title, meta, headings, images, links |
|
crawl |
<url> --pages N [-s selector] [-f format] [-o file] | Follow pagination links, extract from multiple pages |
Output Formats
| Format | Flag | Description |
|---|
| Text | INLINECODE10 | Plain text (default) |
| JSON |
-f json | Structured JSON array |
| CSV |
-f csv | Comma-separated values |
| Markdown |
-f md | Markdown-formatted |
Examples
Extract product listings
uv run --with beautifulsoup4 --with lxml python scripts/scraper.py extract "https://shop.example.com" -s ".product" -f json
Output:
CODEBLOCK2
Extract pricing table
CODEBLOCK3
Get all external links
CODEBLOCK4
Rate Limiting
- - Default: 1 request per second (respectful crawling)
- Override with
--delay 0.5 (seconds between requests) - Respects
robots.txt by default (override with --ignore-robots)
Notes
- - Requires
beautifulsoup4 and lxml (auto-installed by uv run --with) - Uses a standard browser User-Agent to avoid blocks
- Handles redirects, encoding detection, and error pages gracefully
- No JavaScript rendering (use for static HTML pages)
智能网页抓取器
将网页中的结构化数据提取为干净的JSON或CSV格式。
快速开始
bash
抓取页面,提取所有文本内容
uv run --with beautifulsoup4 --with lxml python scripts/scraper.py extract https://example.com
使用CSS选择器提取特定元素
uv run --with beautifulsoup4 --with lxml python scripts/scraper.py extract https://example.com/products -s .product-card
自动检测并提取表格
uv run --with beautifulsoup4 --with lxml python scripts/scraper.py tables https://example.com/pricing
提取页面中的所有链接
uv run --with beautifulsoup4 --with lxml python scripts/scraper.py links https://example.com
提取结构化数据(标题、元数据、标题、链接)
uv run --with beautifulsoup4 --with lxml python scripts/scraper.py structure https://example.com
输出为JSON格式
uv run --with beautifulsoup4 --with lxml python scripts/scraper.py extract https://example.com -s .item -f json
输出为CSV格式
uv run --with beautifulsoup4 --with lxml python scripts/scraper.py extract https://example.com -s table tr -f csv
保存到文件
uv run --with beautifulsoup4 --with lxml python scripts/scraper.py extract https://example.com -s .product -f json -o products.json
多页面抓取(跟随分页)
uv run --with beautifulsoup4 --with lxml python scripts/scraper.py crawl https://example.com/page/1 --pages 5 -s .article
命令
| 命令 | 参数 | 描述 |
|---|
| extract | <url> [-s 选择器] [-f 格式] [-o 文件] | 提取内容,可选通过CSS选择器过滤 |
| tables |
[-f 格式] [-o 文件] | 自动检测并提取所有HTML表格 |
| links | [--external] [--internal] | 提取所有链接(href + 文本) |
| structure | | 提取页面结构:标题、元数据、标题、图片、链接 |
| crawl | --pages N [-s 选择器] [-f 格式] [-o 文件] | 跟随分页链接,从多个页面提取内容 |
输出格式
| 格式 | 标志 | 描述 |
|---|
| 文本 | -f text | 纯文本(默认) |
| JSON |
-f json | 结构化JSON数组 |
| CSV | -f csv | 逗号分隔值 |
| Markdown | -f md | Markdown格式 |
示例
提取产品列表
bash
uv run --with beautifulsoup4 --with lxml python scripts/scraper.py extract https://shop.example.com -s .product -f json
输出:
json
[
{text: Widget Pro - $29.99, tag: div, class: product},
{text: Widget Max - $49.99, tag: div, class: product}
]
提取定价表格
bash
uv run --with beautifulsoup4 --with lxml python scripts/scraper.py tables https://example.com/pricing -f csv
获取所有外部链接
bash
uv run --with beautifulsoup4 --with lxml python scripts/scraper.py links https://example.com --external
速率限制
- - 默认:每秒1个请求(尊重性抓取)
- 使用 --delay 0.5 覆盖(请求之间的秒数)
- 默认遵守 robots.txt(使用 --ignore-robots 覆盖)
注意事项
- - 需要 beautifulsoup4 和 lxml(由 uv run --with 自动安装)
- 使用标准浏览器用户代理以避免被屏蔽
- 优雅处理重定向、编码检测和错误页面
- 不支持JavaScript渲染(适用于静态HTML页面)