javascript - 尊重 robots.txt 文件的 Puppeteer 代码
问题描述
这在 Scrapy Scrapy 和尊重 robots.txt中似乎是可能的,但是在 Puppeteer 中是否有一种简单的方法可以做到这一点?
我还没有找到一种简单的方法来将“尊重机器人”构建到 Puppeteer 命令中。
解决方案
我不相信 puppeteer 内置任何东西,但您可以使用 puppeteer 进行访问robots.txt
,然后使用许多npm
模块中的任何一个进行解析robots.txt
,以查看是否允许您获取任何特定的 URL。例如,以下是您可以使用robots-txt-parser 的方式:
const robotsParser = require('robots-txt-parser')
const robots = robotsParser()
// Now inside an async function
// (or not if using a version of Node.js that supports top-level await)
await robots.useRobotsFor('https://example.com/')
if (await robots.canCrawl(urlToVisit)) {
// Do stuff with puppeteer here to visit the URL
} else {
// Inform the user that sadly crawling that URL is forbidden
}
推荐阅读
- javascript - 当我使用 Vue 应用程序包装时,为什么 React onclick 事件没有触发?(没有其他库)
- postgresql - 如何强制使用具有唯一值 postgresql 的唯一列
- python - python中的线程,没有错误,但程序似乎不起作用
- python-3.x - 自动修复扫描数据中的卡住像素
- java - 在 Windows 上规范化路径时,Java 是否需要支持 ERROR_NO_MORE_FILES?
- list - Flutter:StringBuilder 中的排序列表
- excel - 下标超出范围的问题(错误 9)
- php - 根据WordPress中带有计数器的帖子数量添加图像大小
- express - 将正文大于 64k 的 POST 发送到 Express.js 无法处理
- javascript - 使用 readline 读取两个字符串