JS和Node.js的部分操作技巧
2017-12-03 11:23
232 查看
数据类型。
数据类型和json相互转换。
mysql数据库连接。
http server提取参数。
模拟发送http请求
使用cheerio解析和操作dom
使用http请求数据,使用cheerio解析页面内容。
提取时间,格式化时间。
做项目的时候,经常会遇到时间戳和时间转换的问题。
js里边有一个Date对象,Date对象是Javascript提供的日期和时间的操作接口。
new Date(milliseconds):Date对象接受从1970年1月1日00:00:00 UTC开始计算的毫秒数作为参数。这意味着如果Unix时间戳作为参数,必须将Unix时间戳乘以1000。
unix时间戳:从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,注意是秒,而date对象接收的是毫秒。
对象接收一个标准的日期格式字符串,将其转换成对应时间。
返回的是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数。
我们获取以字符串形式给予的日期的时间戳如下:
Date.now()如何获取当前时间的时间戳,输出是毫秒。
下载数据,按照html的dom格式解析
注意:其中的each函数是jquery提供的函数,用于遍历数组,和原生的forEach相似。
node操作mysql数据库
不废话,直接说怎么用吧:
数据类型和json相互转换。
mysql数据库连接。
http server提取参数。
模拟发送http请求
var request=require("request"); console.log(request); request.get("http://www.baidu.com/", function(error, response, body){ console.log(body); var reqexp=/(http[s]?:\/\/[^'"]+)/g; while(res=reqexp.exec(body)){ console.log(res[0]); } });
使用cheerio解析和操作dom
var cheerio=require("cheerio"); console.log(cheerio); var $=cheerio.load('<div class="container"><div>'); var container=$('.container'); for(var i=0; i<10; i++){ var item=$('<div />'); item.addClass('item'); item.text(i); container.append(item); } console.info($.html());
使用http请求数据,使用cheerio解析页面内容。
http.get('http://www.gongjuji.net', function(req, res){ var result=""; req.on('data', function(data){ result+=data; }); req.on('end', function(){ //console.log(result); parseHtml(result); }); }); function parseHtml(result){ //console.log(result); var $=cheerio.load(result); console.log($.html()); console.log($.text()); console.log($.text().replace(/\s+/g, ' ')); console.log($.text().replace(/[ ]+/g, ' ')); console.log($.text().replace(/(\r\n)+[ ]+/g,'\r\n').replace(/(\r\n)+/g, '\r\n')); console.log($.text().replace(/(\r\n)+/g, '\r\n')); var captionList=$('.body-content .caption'); console.log(captionList); var itemList=[]; captionList.each(function(item){ var cap=$(this); var item={ title:cap.find('h3').text(), linkUrl:cap.find('a').attr('href') } itemList.push(item); }); console.info(itemList); }
提取时间,格式化时间。
做项目的时候,经常会遇到时间戳和时间转换的问题。
js里边有一个Date对象,Date对象是Javascript提供的日期和时间的操作接口。
new Date(milliseconds):Date对象接受从1970年1月1日00:00:00 UTC开始计算的毫秒数作为参数。这意味着如果Unix时间戳作为参数,必须将Unix时间戳乘以1000。
unix时间戳:从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,注意是秒,而date对象接收的是毫秒。
new Date("2013-02-16 21:00:00")
对象接收一个标准的日期格式字符串,将其转换成对应时间。
date.getTime()
返回的是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数。
我们获取以字符串形式给予的日期的时间戳如下:
var date=new Date("2013-02-15 21:00:00"); var time=date.getTime()/1000;
Date.now()如何获取当前时间的时间戳,输出是毫秒。
Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } console.log(new Date().Format("yyyy-MM-dd hh:mm:ss"));
console.log(new Date()); console.log(new Date("2013-02-15 21:00:00")); console.log(new Date(1212121)); console.log(new Date().getTime()); var date = new Date("2013-02-15 21:00:00"); var time = date.getTime()/1000;//转换成秒; 我们再将其转换成日期格式的 var date = new Date( time * 1000 );//.转换成毫秒 var time = date.getFullYear() + "-" + (date.getMonth() < 10 ? '0' + (date.getMonth()+1) : (date.getMonth()+1)) + "-" + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) ; console.log(time); //Date.now()获取当前时间的时间戳,输出是毫秒。 console.log(Date.now());
下载数据,按照html的dom格式解析
var request=require('request'); var cheerio=require('cheerio'); request.get('https://fund.jd.com/fund/detail/tab/netvalue?fundCode=000001&pageIndex=2', function(error, response, body){ console.log(body); var $=cheerio.load(body); console.log($('table tbody').html()); var trlist=$('table tbody').find('tr'); console.log(trlist); trlist.each(function(item){ var tr=$(this); console.log(tr.text()); var tdlist=tr.find('td'); tdlist.each(function(item){ //console.log(item); var td=$(this); if(item==0){ console.log("0 : ",td.text()); }else if(item==1){ console.log("1 : ",td.text()); }else if(item==2){ console.log("2 : ",td.text()); }else if(item==3){ console.log("3 : ",td.text().replace(/%/g,'')); }else{ console.log("Unknow : ",td.text()); } }); }); });
注意:其中的each函数是jquery提供的函数,用于遍历数组,和原生的forEach相似。
node操作mysql数据库
var mysql=require('mysql'); var TEST_DATABASE='scan_data'; var TEST_TABLE='scan'; var client=mysql.createConnection({ host:'192.168.0.250', user:'scan', password:'scanpassword' }); client.connect(); client.query('use '+TEST_DATABASE); client.query( 'select * from '+TEST_TABLE, function selectCb(err, results, fileds){ if(err){ throw err; } if(results){ for(var i=0; i< results.length; i++){ console.log("%d", results[i].id); } } client.end(); } )
var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '123456', database : 'test' }); connection.connect(); connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) { if (error) throw error; console.log('The solution is: ', results[0].solution); });
数组
处理数组类型数据时forEach方法会很方便不废话,直接说怎么用吧:
var arr=['刘翔','刘洋','陈一冰','叶诗文'] arr.forEach(function(name){ //name就是arr数组中对应的元素 });
字典
处理字典类型的key-value对var dict={ key1:'value1', key2:'value2' }; console.log(dict); for(var key in dict){ console.log(key,dict[key]); }
相关文章推荐
- Node.js静态文件服务器实战(接触nodejs开发中比较实用的部分技巧,获益匪浅)
- 13 款惊艳的 Node.js 框架——第2部分
- 提高Node.js性能的应用技巧分享
- node.js 操作 postgresql 数据库
- Node.js MongoDB的安装 配置 数据库的CRUD操作 索引 学习笔记文档
- [Node.js]操作mysql
- Node.js的DAO层,关于数据库操作的db.js
- Node.js使用Mongoose包操作MongoDB数据库
- 夺命雷公狗---node.js---12之fs模块文件的操作
- C#数据库编程基础之数据库配置及部分操作数据库技巧
- edge.js架起node.js和.net互操作桥梁
- js dom 操作技巧
- node.js操作mongoDB数据库示例分享
- Node.js的mysql数据库操作
- node.js操作相关
- node.js fs文件 操作
- node.js下mongoose简单操作实例
- 分针网—每日分享:安装使用Mongoose配合Node.js操作MongoDB的基础教程
- node.js下mongoose简单操作实例
- JS中常用操作技巧(页面动态增加div,contain方法扩展,JSON对象鱼字符串转换)