安裝 npm install request
先引入需要的文件
var request = require('request');
var http = require("http")
const fs = require('fs')
const cheerio = require('cheerio')
var path = require('path')
以上是引入 所需要的包 用到那個引入那個
大概流程就是 請求 拿數據 分析數據 獲取需要的資源 保存資源
var request = require('request');
var http = require("http")
const fs = require('fs')
const cheerio = require('cheerio')
var path = require('path')
function runCode(id) {
id++
request({
url: 'https://www.ivsky.com/bizhi/nvxing',
method: "get",
json: true,
headers: {
"content-type": "application/json",
},
}, function (error, response, body) {
let $ = cheerio.load(body);
$(".il_img a").each((index, item) => {
let urlsrc = 'https://www.ivsky.com' + $(item).attr("href")
requestSrc(urlsrc, id)
})
});
}
function requestSrc(url, id) {
request({
url: url,
method: "get",
json: true,
headers: {
"content-type": "application/json",
},
}, function (error, response, body) {
let $ = cheerio.load(body);
$(".pli img").each((index, item) => {
saveImg('http:' + $(item).attr('src'), index + '.jpg')
})
setTimeout(function () {
console.log("正在保存" + id)
runCode(id);
}, 5000)
});
}
function saveImg(url, name) {
// console.log("正在保存" + name)
var num = parseInt(Math.random() * 10000000);
// console.log(num)
request(url).pipe(fs.createWriteStream(__dirname + '/imgs' + num + name));
}
runCode(1);
純屬娛樂 如有侵權 請告知