const puppeteer = require("puppeteer");
const fs = require("fs-extra");
const jschardet = require("jschardet");
const url = "https://www.yibaixun.com/";
(async () => {
const browser = await puppeteer.launch({
headless: true,
userDataDir: "./data",
});
const page = await browser.newPage();
page.on("response", async (response) => {
const file = './www/';
const responseUrl = response.url();
const fileType = response.headers()['content-type'];
const text = await response.buffer();
const encoding = jschardet.detect(text).encoding;
if (encoding == 'UTF-8') {
console.log('UTF-8=', responseUrl);
}
if (encoding == 'ascii') {
console.log('ascii=', responseUrl);
}
if (encoding == 'ISO-8859-2') {
console.log('ISO-8859-2=', responseUrl);
}
if (encoding == 'windows-1252') {
console.log('windows-1252=', responseUrl);
}
let name = responseUrl.match(new RegExp(`${url}(\\S*)`));
if (name == null) {
const oss = 'https://oss.yibaixun.com/';
name = responseUrl.match(new RegExp(`${oss}(\\S*)`))[1];
} else {
name = name[1];
}
if (fileType == 'text/html') {
fs.outputFileSync(`${file}index.html`, text);
} else {
fs.outputFileSync(`${file}${name}`, text);
}
});
const data = await page.goto(url);
const c = await page.screenshot({ path: "example.png" });
const dimensions = await page.evaluate(() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio,
};
});
const html = await page.content();
const file = "./index.html";
fs.outputFileSync(file, html);
await browser.close();
})();
|