AnyCrawl入门教程:10分钟学会网页内容提取
【免费下载链接】AnyCrawlAnyCrawl 🚀: A Node.js/TypeScript crawler that turns websites into LLM-ready data and extracts structured SERP results from Google/Bing/Baidu/etc. Native multi-threading for bulk processing.项目地址: https://gitcode.com/gh_mirrors/an/AnyCrawl
AnyCrawl是一款基于Node.js/TypeScript的网页爬虫工具,能够将网站转换为LLM就绪数据,并从谷歌、必应、百度等搜索引擎提取结构化的SERP结果,支持原生多线程批量处理,让网页内容提取变得简单高效。
快速了解AnyCrawl核心功能
AnyCrawl主要提供三大核心功能,满足不同的网页数据处理需求:
- Scrape(单页提取):通过
/v1/scrape接口提取单个网页内容,适合获取特定页面的详细信息。 - Crawl(多页爬取):借助
/v1/crawl接口从种子URL开始发现并处理多个页面,支持异步处理,能快速获取job_id并跟踪爬取进度。 - Search(搜索结果提取):利用
/v1/search接口从多个搜索引擎获取搜索结果,返回经过AI处理和分析优化的干净结构化数据,专为大语言模型构建。
简单三步开始使用AnyCrawl
第一步:获取API密钥
在使用AnyCrawl的API之前,需要先获取API密钥。可以通过运行项目中的脚本生成,具体路径为apps/api/scripts/run-generate-api-key.mjs。
第二步:安装依赖
首先克隆仓库,仓库地址是 https://gitcode.com/gh_mirrors/an/AnyCrawl,然后进入项目目录,安装所需依赖:
git clone https://gitcode.com/gh_mirrors/an/AnyCrawl.git cd AnyCrawl pnpm install第三步:发送API请求提取内容
使用curl命令或代码发送POST请求到相应接口即可提取网页内容。以下是使用/v1/scrape接口提取单个网页内容的示例:
curl示例:
curl -X POST "https://api.anycrawl.dev/v1/scrape" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{"url": "https://example.com", "extract_source": "markdown"}'JavaScript示例:
const response = await fetch("https://api.anycrawl.dev/v1/scrape", { method: "POST", headers: { "Content-Type": "application/json", "Authorization": "Bearer YOUR_API_KEY" }, body: JSON.stringify({ url: "https://example.com", extract_source: "markdown" }) }); const data = await response.json(); console.log(data);利用模板提升提取效率
模板是用于网页提取、爬取或搜索的可重用配置。不必在每次API调用时重复相同的选项,只需在模板中定义一次(或从AnyCrawl模板商店获取),然后通过template_id引用即可。
模板使用示例
curl示例:
curl -X POST "https://api.anycrawl.dev/v1/scrape" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{"url": "https://example.com", "template_id": "content-extractor"}'JavaScript示例:
const response = await fetch("https://api.anycrawl.dev/v1/scrape", { method: "POST", headers: { "Content-Type": "application/json", "Authorization": "Bearer YOUR_API_KEY" }, body: JSON.stringify({ url: "https://example.com", template_id: "content-extractor" }) }); const data = await response.json(); console.log(data);处理提取结果
AnyCrawl返回的结果是结构化数据,方便进一步处理和分析。对于搜索结果,还可以提供scrape_options对每个搜索结果URL进行抓取,获取更详细的信息。
提取结果中包含extractedData等字段,可根据需求对其进行后处理,添加自定义字段等操作。相关的处理逻辑可以参考packages/scrape/src/core/DataExtractor.ts。
通过以上简单步骤,就能快速上手AnyCrawl进行网页内容提取,无论是单页提取还是多页爬取,都能高效完成,为后续的数据分析和处理提供有力支持。更多详细功能和配置选项可以查阅项目的官方文档。
【免费下载链接】AnyCrawlAnyCrawl 🚀: A Node.js/TypeScript crawler that turns websites into LLM-ready data and extracts structured SERP results from Google/Bing/Baidu/etc. Native multi-threading for bulk processing.项目地址: https://gitcode.com/gh_mirrors/an/AnyCrawl
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考