练习地址: https://wallhaven.cc/toplist
const express = require('express');
const axios = require('axios');
const cheerio = require('cheerio');
const schedule = require('node-schedule');
const fs = require('fs');
async function downloadImage(url) {
const response = await axios.get(url, {responseType: 'arraybuffer'});
return response.data;
}
async function saveImage(url, path) {
const image = await downloadImage(url);
fs.writeFileSync(path, image);
}
async function AccessToResources(url) {
try {
const response = await axios.get(url);
const $ = cheerio.load(response.data);
const images = [];
$('a.preview').each((i, el) => {
const img = $(el).attr('href');
if (!img) return;
images.push(img);
});
for (let i = 0; i < images.length; i++) {
const date = new Date();
const response = await axios.get(images[i]);
const $ = cheerio.load(response.data);
const img = $('img#wallpaper').attr('src');
const fileName = date.getTime();
await saveImage(img, `./images/${fileName}.jpg`)
}
} catch (e) {
console.log('e');
}
}
const rule = new schedule.RecurrenceRule();
rule.hour = [1, 2, 3, 4, 5, 6];
rule.minute = 0;
const url = 'https://wallhaven.cc/toplist';
const job = schedule.scheduleJob(rule, function () {
console.log('开始执行任务', rule.hour)
AccessToResources(url);
});
AccessToResources(url);
const app = express();
app.use(express.static('images'));
app.get('/', (req, res) => {
res.send('Hello World!');
})
app.listen(1234, () => {
console.log('server is running at http://localhost:1234');
})