使用Node.js中的Website Scraper
1.安装website-scraper
使用基于Puppeteer(Chromium的无头版本)的脚本的最有价值的优势之一是,您不仅能够复制实现纯JavaScript甚至jQuery的静态网站功能,而且还能够下载由使用React或angular的动态页面生成的内容和资源。在终端中使用npm安装website-scraper-puppeteer库:
npm install website-scraper website-scraper-puppeteer有关此项目的更多信息,请访问Github上的官方存储库。此插件基本上以无头模式启动Chromium,该模式只是打开页面并等待直到整个页面加载完毕。这远非理想,因为您可能需要等到加载某些资源或单击某个按钮或登录后再进行操作。
2.编写下载脚本
使用NPM将插件安装到您的工作目录后,您只需创建带有代码的javascript文件即可下载某些网站。创建index.js文件并将以下代码放入其中:
// index.js
const scrape = require('website-scraper');
const PuppeteerPlugin = require('website-scraper-puppeteer');
const path = require('path');
scrape({
// Provide the URL(s) of the website(s) that you want to clone
// In this example, you can clone the Our Code World website
urls: ['https://ourcodeworld.com/'],
// Specify the path where the content should be saved
// In this case, in the current directory inside the ourcodeworld dir
directory: path.resolve(__dirname, 'bfw'),
// Load the Puppeteer plugin
plugins: [
new PuppeteerPlugin({
launchOptions: {
// If you set this to true, the headless browser will show up on screen
headless: true
}, /* optional */
scrollToBottom: {
timeout: 10000,
viewportN: 10
} /* optional */
})
]
});
前面的代码将导入已安装的库和Node.js路径帮助器(以创建当前目录的绝对路径)。我们将调用scrape方法,将第一个参数提供一个具有所需配置的对象,以开始网站克隆。最重要的选项是urls属性,该属性需要一个字符串数组,其中每个项目都是您要克隆的网站页面的Web URL。目录选项对应于应放置网站内容的本地目录路径。plugins选项将为常规抓取程序加载puppeteer插件,以便正确克隆动态网站。
3.运行脚本
最后,打开终端并切换到您刚刚编写的脚本的目录并执行:
node index.jsok,整个网站就克隆下来了
网友回复
python能写一个检测nginx rewrite高危漏洞的工具代码?
css如何给video视频进行mask遮罩?
windows如何同时允许两个用户远程桌面连接同一个电脑?
nginx升级到1.30.1导致无法启动 [emerg] SSL_CTX_new() failed怎么办?
什么是ASLR(地址随机化)?
有没有不依赖embedding向量的RAG技术?
有没有支持实时打断语音通话并后台帮你执行任何的ai模型?
开源ai大模型文件格式GGUF、MLX、Safetensors、 ONNX 有什么区别?
出海挣钱支付收款PayPal、Wise 、PingPong、Stripe如何选择?
如何实现类似google的图片隐形水印添加和识别技术?


