为什么 openGraphScraper 是 Node.js 开发者必备的元数据抓取工具【免费下载链接】openGraphScraperNode.js scraper service for Open Graph Info and More!项目地址: https://gitcode.com/gh_mirrors/op/openGraphScraperopenGraphScraper 是一款功能强大的 Node.js 元数据抓取工具它能够轻松提取网站的 Open Graph、Twitter Card 等各类元数据为开发者构建社交媒体分享功能、内容预览系统提供了极大便利。无论是开发社交应用、内容管理系统还是营销工具这款工具都能帮助你快速获取所需的网页元数据信息。快速上手简单几步即可实现元数据抓取 要开始使用 openGraphScraper只需通过 npm 安装即可npm install open-graph-scraper --save安装完成后你可以使用以下简单代码实现元数据抓取const ogs require(open-graph-scraper); const options { url: http://ogp.me/ }; ogs(options) .then((data) { const { error, html, result, response } data; console.log(result:, result); // 包含所有 Open Graph 结果 })这段代码将返回类似以下的结果{ ogTitle: Open Graph protocol, ogType: website, ogUrl: https://ogp.me/, ogDescription: The Open Graph protocol enables any web page to become a rich object in a social graph., ogImage: [ { height: 300, type: image/png, url: https://ogp.me/logo.png, width: 300 } ], charset: utf-8, requestUrl: http://ogp.me/, success: true }强大功能满足多样化元数据抓取需求openGraphScraper 提供了丰富的选项让你可以根据实际需求定制抓取行为核心配置选项名称说明默认值是否必填url目标网站 URL是html直接传入 HTML 字符串进行解析不与 url 同时使用否fetchOptionsFetch API 配置选项{}否timeout请求超时时间秒10否blacklist不希望抓取的网站列表[]否onlyGetOpenGraphInfo仅抓取 Open Graph 信息不使用其他回退机制false否customMetaTags自定义需要抓取的元标签[]否urlValidatorSettingsURL 验证器配置查看默认配置否高级应用场景1. 自定义元标签抓取如果你需要抓取一些非标准的元标签可以使用customMetaTags选项const options { url: https://github.com/jshemas/openGraphScraper, customMetaTags: [{ multiple: false, property: hostname, fieldName: hostnameMetaTag, }], }; ogs(options) .then((data) { const { result } data; console.log(hostnameMetaTag:, result.customMetaTags.hostnameMetaTag); // hostnameMetaTag: github.com })2. 直接解析 HTML 字符串如果你已经有了 HTML 内容可以直接传入进行解析无需再次请求const options { html: htmlhead meta propertyog:title contentfoobar / meta propertyog:type contentwebsite / /head/html }; ogs(options) .then((data) { const { result } data; console.log(result:, result); })3. 自定义请求头有些网站可能会阻止默认的请求头这时你可以通过fetchOptions自定义用户代理const userAgent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36; ogs({ url: https://www.wikipedia.org/, fetchOptions: { headers: { user-agent: userAgent } } }) .then((data) { // 处理结果 })开始使用 openGraphScraper要开始使用 openGraphScraper首先克隆项目仓库git clone https://gitcode.com/gh_mirrors/op/openGraphScraper项目提供了一个示例应用你可以通过以下步骤运行cd openGraphScraper/example npm ci npm run start然后在浏览器中访问http://localhost:3000/scraper?urlhttp://ogp.me/即可测试元数据抓取功能。总结为什么选择 openGraphScraperopenGraphScraper 凭借其简单易用的 API、丰富的功能选项和稳定的性能成为 Node.js 开发者抓取元数据的理想选择。它不仅支持标准的 Open Graph 和 Twitter Card 元数据还允许自定义抓取规则满足各种复杂场景需求。无论你是构建社交媒体应用、内容管理系统还是营销工具openGraphScraper 都能帮助你快速、高效地获取网页元数据提升开发效率。如果你正在寻找一款可靠的元数据抓取工具不妨试试 openGraphScraper相信它会成为你项目中的得力助手【免费下载链接】openGraphScraperNode.js scraper service for Open Graph Info and More!项目地址: https://gitcode.com/gh_mirrors/op/openGraphScraper创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考