时间:2021-07-01 10:21:17 帮助过:23人阅读
2.在 lib 里面新建 sql.js
var mysql = require(‘mysql‘); function SQL(callback) { var connection = mysql.createConnection({ host: ‘‘, user: ‘root‘, password: ‘‘, database: ‘‘ }); connection.connect(); callback.call(null, connection); connection.end(); } module.exports = { query: function(str, callback) { SQL(function(connection) { connection.query(str, callback); }); } };
3.node 引用 mysql
var request = require(‘superagent‘); var cheerio = require(‘cheerio‘) var sql = require(‘../lib/sql.js‘) request .get(‘http://‘) .end(function(err, res) { if (err) { console.log(err); return; } var $ = cheerio.load(res.text); var cateDiv = $(‘.list-category-nav .unstyled.ui-dropdown-menu a‘); cateDiv.each(function(index, item) { var text = $(item).text(); var href = $(item).attr(‘href‘); console.log(text, href); sql.query(‘INSERT INTO cate1 (name, url) VALUES ("‘ + text + ‘", "‘ + href + ‘")‘); }); });
node + mysql
标签:category ack pen sql function echo .text callback package