node.js爬虫,爬到数据存入数据库
引入包
const express = require('express');
const request = require('request');
const cheerio = require('cheerio')
const mysql = require('mysql');
const app = express();
// 第一步 配置数据库连接
var pool = mysql.createPool({
host : 'localhost',
user : 'root',
password : 'root',
database : 'shop'
});
//http://localhost:8081/index接口地址
app.get('/index', function (req, res) {
request(“需要爬取的网站”, function (error, response, body) {
var $ = cheerio.load(body)
var list = $('.container .main .sct4 #table1 tr')
var jobs= [];
list.each(function(){
jobs.push({
name:$(this).find('td').text()
})
// 插入数据库开始
var test= jobs[0].name.replace(/\s+/g,"")//去除数据中的空格
var sql = `INSERT INTO pash (name) VALUE ('${test}')`
pool.query(sql, function (error, results, fields) {
if (error) throw error;
});
//插入结束
})
res.json(jobs)
});
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("访问地址为:",host,port)
})