+
80
-

Node.js如何克隆一个网站?

Node.js如何克隆一个网站?把这个网站的html、css、图片、js、字体全部下载下来?

网友回复

+
0
-

使用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.js

ok,整个网站就克隆下来了
我知道答案,我要回答