nodejs入门教程之http的get和request简介及应用

nodejs入门教程之http的get和request简介及应用
前言
上一篇文章,我介绍了nodejs的几个经常使用的模块及简单的案例,今天咱们再来重点看一下nodejs的http模块,关于http模块,咱们能够看下nodejs官方文档。关于http模块,有兴趣的能够研究一下node的源码。http模块功能是很强大的,今天主要介绍他的get和request方法!

GET简介
咱们首先来运行一下下面的代码

const http = require("http")
http.get('http://www.baidu.com', (res) => {
  console.log(`Got response: ${res.statusCode}`);
  // consume response body
  res.resume();
}).on('error', (e) => {
  console.log(`Got error: ${e.message}`);
});
会返回一个200的状态码!

将上面代码稍微改进一下。

const http = require("http") 
const url = "http://www.haorooms.com/post/nodejs_rmyyong" 
http.get(url,(res)=>{
    var html = ""
    res.on("data",(data)=>{
        html+=data
    })

    res.on("end",()=>{
        console.log(html)
    })
}).on("error",(e)=>{
    console.log(`获取数据失败: ${e.message}`)
})
运行一下这段代码,会怎么样?会把我这个页面大源码给爬下来了!

也就是说,咱们能够利用http的get方法,写一个爬虫,来爬取网页数据!(不少网页爬虫都是用python写的)咱们前端也能够用node写网页爬虫,来爬取数据!固然,咱们来要对爬来的数据进行筛选和整合,筛选出咱们想要的数据!咱们能够引用cheerio,进行数据的筛选。爬取网页数据呢,能够配合nodejs的Promise对象,Promise对象是ES6的一个新的对象,最先是社区里面先提出来的,后来,jquery deferred等都引入关于jquery的deferred,我以前也写过一篇文章http://www.haorooms.com/post/jquery_deferred_img 有兴趣的能够看一下!

写爬虫代码,我在这里就不展开了,感兴趣的能够关注个人github,我会写一个简单的放上去,你们能够参考(ps暂时尚未写哦)。

request简介
http的request也很厉害!官方这么描述“This function allows one to transparently issue requests.”他的官方案例以下:

var postData = querystring.stringify({
  'msg' : 'Hello World!'
});
var options = {
  hostname: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': postData.length
  }
};

var req = http.request(options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
  });
  res.on('end', () => {
    console.log('No more data in response.')
  })
});

req.on('error', (e) => {
  console.log(`problem with request: ${e.message}`);
});

// write data to request body
req.write(postData);
req.end();
咱们能够利用这个http的request来提交一下评论,咱们能够获取网站的一些评论接口,经过上面options,咱们能够配置请求的headers信息,进行网站的灌水评论!

经过这个方法,咱们能够写一些网站灌水插件,自动发布网站评论等等!【ps,如今网站大多都有防止灌水的机制!因此你们建议你们不要干坏事哦!!!!】

 

nodejs入门教程之http的get和request简介及应用

2016年4月23日  14523次浏览

前言

上一篇文章,我介绍了nodejs的几个经常使用的模块及简单的案例,今天咱们再来重点看一下nodejs的http模块,关于http模块,咱们能够看下nodejs官方文档。关于http模块,有兴趣的能够研究一下node的源码。http模块功能是很强大的,今天主要介绍他的get和request方法!html

GET简介

咱们首先来运行一下下面的代码前端

const http =require("http") http.get('http://www.baidu.com',(res)=>{ console.log(`Got response: ${res.statusCode}`);// consume response body res.resume();}).on('error',(e)=>{ console.log(`Got error: ${e.message}`);});

会返回一个200的状态码!node

将上面代码稍微改进一下。python

const http =require("http")const url ="http://www.haorooms.com/post/nodejs_rmyyong" http.get(url,(res)=>{var html ="" res.on("data",(data)=>{ html+=data }) res.on("end",()=>{ console.log(html)})}).on("error",(e)=>{ console.log(`获取数据失败: ${e.message}`)})

运行一下这段代码,会怎么样?会把我这个页面大源码给爬下来了!jquery

也就是说,咱们能够利用http的get方法,写一个爬虫,来爬取网页数据!(不少网页爬虫都是用python写的)咱们前端也能够用node写网页爬虫,来爬取数据!固然,咱们来要对爬来的数据进行筛选和整合,筛选出咱们想要的数据!咱们能够引用cheerio,进行数据的筛选。爬取网页数据呢,能够配合nodejs的Promise对象,Promise对象是ES6的一个新的对象,最先是社区里面先提出来的,后来,jquery deferred等都引入关于jquery的deferred,我以前也写过一篇文章http://www.haorooms.com/post/jquery_deferred_img 有兴趣的能够看一下!git

写爬虫代码,我在这里就不展开了,感兴趣的能够关注个人github,我会写一个简单的放上去,你们能够参考(ps暂时尚未写哦)。github

request简介

http的request也很厉害!官方这么描述“This function allows one to transparently issue requests.”他的官方案例以下:网页爬虫

var postData = querystring.stringify({'msg':'Hello World!'});var options ={ hostname:'www.google.com', port:80, path:'/upload', method:'POST', headers:{'Content-Type':'application/x-www-form-urlencoded','Content-Length': postData.length }};var req = http.request(options,(res)=>{ console.log(`STATUS: ${res.statusCode}`); console.log(`HEADERS: ${JSON.stringify(res.headers)}`); res.setEncoding('utf8'); res.on('data',(chunk)=>{ console.log(`BODY: ${chunk}`);}); res.on('end',()=>{ console.log('No more data in response.')})}); req.on('error',(e)=>{ console.log(`problem with request: ${e.message}`);});// write data to request body req.write(postData); req.end();

咱们能够利用这个http的request来提交一下评论,咱们能够获取网站的一些评论接口,经过上面options,咱们能够配置请求的headers信息,进行网站的灌水评论!api

经过这个方法,咱们能够写一些网站灌水插件,自动发布网站评论等等!【ps,如今网站大多都有防止灌水的机制!因此你们建议你们不要干坏事哦!!!!】app

相关文章
相关标签/搜索