练习地址: https://wallhaven.cc/toplist文章来源:https://www.toymoban.com/news/detail-683468.html
文章来源地址https://www.toymoban.com/news/detail-683468.html
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');
})
到了这里,关于Node爬虫项目精简版 wallhaven网站实操 2023.8.29的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!