Node.js爬虫入门指南:使用API方式爬取Wallhaven壁纸信息并存入mysql
const axios = require('axios');
const mysql = require('mysql');
// 连接到MySQL数据库
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'your_password',
database: 'wallhaven'
});
connection.connect();
// 定义一个函数来将数据插入到MySQL数据库
function insertIntoDatabase(data) {
const query = 'INSERT INTO wallpapers (id, url, short_url, views, favorites, category, dimension_x, dimension_y, resolution, file_size, uploaded_at, description) VALUES ?';
connection.query(query, [data], function (error, results, fields) {
if (error) throw error;
// 可以在这里添加日志记录或其他逻辑
console.log('数据插入成功');
});
}
// 发送API请求并处理数据
axios.get('https://wallhaven.cc/api/v1/search?q=anime&sorting=random&page=1')
.then(response => {
const wallpapers = response.data.data;
const dataForDatabase = wallpapers.map(wallpaper => [
wallpaper.id,
wallpaper.path,
wallpaper.short_url,
wallpaper.views,
wallpaper.favorites,
wallpaper.category,
wallpaper.dimension_x,
wallpaper.dimension_y,
wallpaper.resolution,
wallpaper.file_size,
wallpaper.uploaded_at,
wallpaper.description
]);
// 插入数据到数据库
insertIntoDatabase(dataForDatabase);
})
.catch(error => {
console.error('Error fetching data: ', error);
});
// 记得关闭数据库连接
connection.end();
这段代码示例修复了原代码中的问题,并添加了数据库连接的关闭操作。它展示了如何使用Node.js和MySQL模块来处理API响应并将数据存储到数据库中。
评论已关闭