DuckDB爬虫(crawler)扩展
📊 下载与收录
- 周下载量: 383
- GitHub星标: 4
- 扩展仓库 : GitHub
- 社区扩展索引 : DuckDB社区扩展
- 扩展描述(YAML): 支持HTML提取和MERGE操作的SQL原生网页爬虫
维护者: onnimonni
📦 安装与加载
INSTALL crawler FROM community;
LOAD crawler;
💡 快速示例
基础用法
SELECT url, jq(html.document, 'h1').text as title
FROM crawl(['https://example.com']);
使用 read_html(类似Google Sheets的 =IMPORTHTML)
-- 提取表格
SELECT * FROM read_html('https://en.wikipedia.org/wiki/...', 'table.wikitable', 1);
-- 提取JS变量
SELECT * FROM read_html('https://example.com/page', 'js=jobs');
数据提取示例
SELECT
url,
jq(html.document, '.price', 'data-amount') as price,
html.readability.title as article_title
FROM crawl(['https://example.com/products']);
使用 MERGE 进行智能合并
CRAWLING MERGE INTO pages
USING crawl(['https://example.com']) AS src
ON (src.url = pages.url)
WHEN MATCHED THEN UPDATE BY NAME
WHEN NOT MATCHED THEN INSERT BY NAME;
✨ 核心特性
crawl() 表函数 : 支持自动速率限制和 robots.txt 合规性
crawl_url() : 支持 LATERAL 连接
sitemap(): 解析 XML 网站地图
read_html() : 类似 Google Sheets 的 IMPORTHTML,支持提取表格、列表、JS变量
jq() 和 htmlpath(): 基于CSS选择器的数据提取函数
html.readability: 文章内容提取
html.schema: 解析 JSON-LD/微数据
CRAWLING MERGE INTO 语法: 支持"存在则更新,不存在则插入"的智能写入
🛠️ 新增函数
| 函数名 |
函数类型 |
描述 |
备注 |
示例 |
crawl |
表函数 |
NULL |
NULL |
|
crawl_stream |
表函数 |
NULL |
NULL |
|
crawl_url |
表函数 |
NULL |
NULL |
|
css_select |
标量函数 |
NULL |
NULL |
|
discover |
标量函数 |
NULL |
NULL |
|
htmlpath |
标量函数 |
NULL |
NULL |
|
jq |
标量函数 |
NULL |
NULL |
|
read_html |
表函数 |
NULL |
NULL |
|
sitemap |
表函数 |
NULL |
NULL |
|
stream_merge_internal |
表函数 |
NULL |
NULL |
|
⚙️ 新增配置项
| 名称 |
描述 |
输入类型 |
作用域 |
别名 |
crawler_default_delay |
若 robots.txt 未指定,则使用默认爬取延迟(秒) |
DOUBLE |
GLOBAL |
\[\] |
crawler_max_response_bytes |
最大响应体大小(字节,0 = 无限制) |
BIGINT |
GLOBAL |
\[\] |
crawler_respect_robots |
是否遵守 robots.txt 指令 |
BOOLEAN |
GLOBAL |
\[\] |
crawler_timeout_ms |
HTTP 请求超时时间(毫秒) |
BIGINT |
GLOBAL |
\[\] |
crawler_user_agent |
爬虫 HTTP 请求的 User-Agent 字符串 |
VARCHAR |
GLOBAL |
\[\] |
📚 完整文档
请访问:https://github.com/midwork-finds-jobs/duckdb-crawler