node.js爬蟲,爬到資料存入資料庫
阿新 • • 發佈:2018-11-22
引入包
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)
})