http爬某糖页面图片
const https = require('https');
const cheerio = require("cheerio");
const fs = require('fs');
https.get("https://www.duitang.com/category/?cat=beauty",res=>{
let arr = [];
let str = "";
let str1 = ``;
res.setEncoding('utf8');
res.on('data',chunk=>{
str += chunk.toString()
})
res.on("end",()=>{
let $ = cheerio.load(str)
$('img').each(function(i,ele){
arr.push($(ele).attr("src"))
})
arr.forEach(ele => {
str1 += `
<div>
<img src=${ele} alt="图片">
</div>
`
});
str1 = `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="./index.css">
<link rel="stylesheet" href=" ">
</head>
<body>
${str1}
</body>
<script type="text/javascript" src="" ></script>
<script type="text/javascript">
</script>
</html>`
fs.writeFileSync("./a.html",str1)
}).on('error', (e) => {
console.error(`出现错误: ${e.message}`);
});
})