对于服务端渲染的,要解析html,安装cheerio
npm init npm install cheerio --registry=http://registry.npm.taobao.org
var fs = require('fs'); var cheerio = require('cheerio'); var myHtml = fs.readFileSync("a.html"); var $ = cheerio.load(myHtml); var t = $('html').find('hr'); var t2 = t.nextAll(); t2.each(function(i, elem) { getContent($(this)); console.log($(this).text()); // .text()报错,用.children[0].data });
参考:
https://www.jianshu.com/p/629a81b4e013
DOM节点选择参考:https://blog.csdn.net/fbysss/article/details/54798229
NaN处理:
// ES5 isNaN(NaN) //true isNaN(undefined) //true isNaN('qwer') //true isNaN(123) //false //ES6 Number.isNaN(undefined) //false Number.isNaN(NaN) //true Number.isNaN('qwer') //false Number.isNaN(123) //false
参考:https://blog.csdn.net/clydekuo/article/details/54929081
mysql 逐条插入与批量插入
let insertSqlOne = 'INSERT INTO table(id, c_id, name, add, s, create_time, deleted) VALUES (0,2,?,?,?,?,0)' // one record let insertSqlBatch = 'INSERT INTO table(id, c_id, name, add, s, create_time, deleted) VALUES ?' // many record var insertParamsOne = [] var insertParamsBatch = [] c_time = moment().format('YYYY-MM-DD HH:mm:ss') // one by one nameList.each((index, item) => { insertParamsOne = [ 0, TARGET_ID, hn, ha, Number(hs), c_time, 0 ] mysqlConnection.query(insertSql, insertParamsOne, (error, result) => { if(error) { console.log('[INSERT ERROR] - ', error.message) return } console.log('存入数据库: ' + result.affectedRows + '条记录') }) }) // batch nameList.each((index, item) => { var hn = item.children[0].data var ha = addList[index].children[0].data var hs = sList[index].children[0].data insertParamsBatch.push([ 0, TARGET_ID, hn, ha, Number(hs), c_time, 0 ]) }) mysqlConnection.query(insertSqlBatch, [insertParamsBatch], (error, result) => { if(error) { console.log('[INSERT ERROR] - ', error.message) return } console.log('存入数据库: ' + result.affectedRows + '条记录') })
mysql插入前判断:
如下,新插入的数据为第二行SELECT后的内容。判断条件为WHERE NOT EXISTS括号中内容。
如果判断条件查处数据,则不插入。否则,插入新数据。
INSERT INTO x_table (c_id, name, add, s, deleted) SELECT 2, 'namexxx', 'addxxx', 3, 0 FROM DUAL WHERE NOT EXISTS ( SELECT c_id, name, add, s, deleted FROM x_table where c_id=2 AND name='namexxx' AND deleted = 0 )
let insertSql = 'INSERT INTO x_table(c_id, name, add, s, deleted, create_time) SELECT ?,?,?,?,?,? FROM DUAL WHERE NOT EXISTS(SELECT c_id, name, add, s, deleted FROM x_table WHERE c_id = ? AND name = ? AND deleted = ?)' var insertParams = [] c_time = moment().format('YYYY-MM-DD HH:mm:ss') // insertParams = [] xList.forEach((item, index) => { insertParams = [ TARGET_ID, item.name, item.add, Number.isNaN(Number(item.s)) ? null : Number(item.s), 0, c_time, TARGET_ID, item.name, 0 ]
mysqlConnection.query(insertSql, insertParams, (error, result) => { if(error) { console.log('[INSERT ERROR] - ', error.message) return } console.log('存入数据库: ' + result.affectedRows + '条记录') }) })