Node.js 使用http客户端向网站请求数据并保存
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Node.js 使用http客户端向网站请求数据并保存相关的知识,希望对你有一定的参考价值。
app.js代码:
// 内置http模块,提供了http服务器和客户端功能 var http=require("http"); // 内置文件处理模块 var fs=require(‘fs‘); // 创建一个将流数据写入文件的WriteStream对象 var outstream=fs.createWriteStream(‘./retval.html‘); // 请求参数JSON var options={ hostname:‘www.cnblogs.com‘,// 这里别加http://,否则会出现ENOTFOUND错误 port:80, path:‘/xiandedanteng‘,// 子路径 method:‘GET‘, }; // 请求并获得数据 var req=http.request(options,function(resp){ console.log(‘状态码resp.statusCode=‘+resp.statusCode); console.log(‘响应头resp.headers=‘+JSON.stringify(resp.headers)); resp.setEncoding(‘utf8‘); resp.on(‘data‘,function(chunk){ console.log(‘响应内容:‘+chunk); outstream.write(chunk,‘utf8‘); console.log(‘文件写入完毕。‘); }); }); // 超时处理 req.setTimeout(5000,function(){ req.abort(); }); // 出错处理 req.on(‘error‘,function(err){ if(err.code=="ECONNRESET"){ console.log(‘socket端口连接超时。‘); }else{ console.log(‘请求发生错误,err.code:‘+err.code); } }); // 请求结束 req.end();
获得的数据文件如下:
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>逆火狂飙 - 博客园</title> <link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=-wBWg2jMfLNV0-ScpDNxGkoH_gCbdW1yTVJLHzPL7HE1"/> <link id="MainCss" type="text/css" rel="stylesheet" href="/skins/SimpleBlue/bundle-SimpleBlue.css?v=jJERBFSojhmgst84aaRDal9S3q1WoO-WcNudmMzGJS81"/> <link id="mobile-style" media="only screen and (max-width: 768px)" type="text/css" rel="stylesheet" href="/skins/SimpleBlue/bundle-SimpleBlue-mobile.css?v=z0BacpCfWeLlXDCM0C158kTP_DMqMbGBapID4f-QztI1"/> <link title="RSS" type="application/rss+xml" rel="alternate" href="http://www.cnblogs.com/xiandedanteng/rss"/> <link title="RSD" type="application/rsd+xml" rel="EditURI" href="http://www.cnblogs.com/xiandedanteng/rsd.xml"/> <link type="application/wlwmanifest+xml" rel="wlwmanifest" href="http://www.cnblogs.com/xiandedanteng/wlwmanifest.xml"/> <script src="//common.cnblogs.com/script/jquery.js" type="text/javascript"></script> <script type="text/javascript">var currentBlogApp = ‘xiandedanteng‘, cb_enable_mathjax=false;var isLogined=false;</script> <script src="/bundles/blog-common.js?v=zLAewHCaOmEYGOn3iyM1yprOSNEWA2FkU2TsbgxrSSg1" type="text/javascript"></script> </head> <body> <a name="top"></a> <div id="home"> <div id="header"> <div id="blogTitle"> <!--done--> <div class="title"><a id="Header1_HeaderTitle" class="headermaintitle" href="http://www.cnblogs.com/xiandedanteng/">More Powerful</a></div> <div class="subtitle">野生四张屌丝半调子curl程序员的逆袭</div> </div><!--end: blogTitle 博客的标题和副标题 --> <div id="navigator"> <ul id="navList"> <li id="nav_sitehome"></li> <li id="nav_myhome"><a id="blog_nav_myhome" class="menu" href="http://www.cnblogs.com/xiandedanteng/">首页</a></li> <li id="nav_newpost"><a id="blog_nav_newpost" class="menu" rel="nofollow" href="https://i.cnblogs.com/EditPosts.aspx?opt=1">新随笔</a></li> <li id="nav_contact"><a id="blog_nav_contact" class="menu" rel="nofollow" href="https://msg.cnblogs.com/send/%E9%80%86%E7%81%AB%E7%8B%82%E9%A3%99">联系</a></li> <li id="nav_rss"><a id="blog_nav_rss" class="menu" href="http://www.cnblogs.com/xiandedanteng/rss">订阅</a> <!--<a id="blog_nav_rss_image" class="aHeaderXML" href="http://www.cnblogs.com/xiandedanteng/rss"><img src="//www.cnblogs.com/images/xml.gif" alt="订阅" /></a>--></li> <li id="nav_admin"><a id="blog_nav_admin" class="menu" rel="nofollow" href="https://i.cnblogs.com/">管理</a></li> </ul> <div class="blogStats"> <div id="blog_stats"> <!--done--> 随笔-504 文章-0 评论-111 </div> </div><!--end: blogStats --> </div><!--end: navigator 博客导航栏 --> </div><!--end: header 头部 --> <div id="main"> <div id="mainContent"> <div class="forFlow"> <!--done--> <div class="day"> <div class="dayTitle"> <a id="homepage1_HomePageDays_DaysList_ctl00_ImageLink" href="http://www.cnblogs.com/xiandedanteng/archive/2017/09/17.html">2017年9月17日</a> </div> <div class="postTitle"> <a id="homepage1_HomePageDays_DaysList_ctl00_DayList_TitleUrl_0" class="postTitle2" href="http://www.cnblogs.com/xiandedanteng/p/7534689.html">Node.js 使用http客户端向网站请求数据并显示</a> </div> <div class="postCon"><div class="cnblogs-post-body" id="postlist_postbody_7534689">正文内容加载中...</div><script type="text/javascript">getBlogPostBody(7534689);</script></div> <div class="clear"></div> <div class="postDesc">posted @ 2017-09-17 08:42 逆火狂飙 阅读(0) 评论(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=7534689" rel="nofollow">编辑</a></div> <div class="clear"></div> </div> <div class="day"> <div class="dayTitle"> <a id="homepage1_HomePageDays_DaysList_ctl01_ImageLink" href="http://www.cnblogs.com/xiandedanteng/archive/2017/09/16.html">2017年9月16日</a> </div> <div class="postTitle"> <a id="homepage1_HomePageDays_DaysList_ctl01_DayList_TitleUrl_0" class="postTitle2" href="http://www.cnblogs.com/xiandedanteng/p/7533330.html">近来这些事</a> </div> <div class="postCon"><div class="c_b_p_desc">摘要: 8.21-8.26 Angularjs 8.27-9.8 Canvas 9.9- 9.12 Git,SpringBoot 9.13-9.16 Node.js<a href="http://www.cnblogs.com/xiandedanteng/p/7533330.html" class="c_b_p_desc_readmore">阅读全文</a></div></div> <div class="clear"></div> <div class="postDesc">posted @ 2017-09-16 22:54 逆火狂飙 阅读(2) 评论(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=7533330" rel="nofollow">编辑</a></div> <div class="clear"></div> <div class="postSeparator"></div> <div class="postTitle"> <a id="homepage1_HomePageDays_DaysList_ctl01_DayList_TitleUrl_1" class="postTitle2" href="http://www.cnblogs.com/xiandedanteng/p/7531609.html">Node.js 文件系统流pipe到Http响应流中</a> </div> <div class="postCon"><div class="c_b_p_desc">摘要: // 内置http模块,提供了http服务器和客户端功能(path模块也是内置模块,而mime是附加模块) var http=require("http"); var fs=require("fs"); var _dirname="./html" // 创建服务器,创建HTTP服务器要调用http.createServer()函数,它只有一个参数,是个回调函数,服务器每次收到http请求后都会...<a href="http://www.cnblogs.com/xiandedanteng/p/7531609.html" class="c_b_p_desc_readmore">阅读全文</a></div></div> <div class="clear"></div> <div class="postDesc">posted @ 2017-09-16 16:07 逆火狂飙 阅读(1) 评论(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=7531609" rel="nofollow">编辑</a></div> <div class="clear"></div> </div> <div class="day"> <div class="dayTitle"> <a id="homepage1_HomePageDays_DaysList_ctl02_ImageLink" href="http://www.cnblogs.com/xiandedanteng/archive/2017/09/15.html">2017年9月15日</a> </div> <div class="postTitle"> <a id="homepage1_HomePageDays_DaysList_ctl02_DayList_TitleUrl_0" class="postTitle2" href="http://www.cnblogs.com/xiandedanteng/p/7529088.html">Node.js 使用angularjs取得Nodejs http服务端返回的JSON数组示例</a> </div> <div class="postCon"><div class="c_b_p_desc">摘要: server.js代码: 页面Angularjs代码:<a href="http://www.cnblogs.com/xiandedanteng/p/7529088.html" class="c_b_p_desc_readmore">阅读全文</a></div></div> <div class="clear"></div> <div class="postDesc">posted @ 2017-09-15 23:13 逆火狂飙 阅读(1) 评论(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=7529088" rel="nofollow">编辑</a></div> <div class="clear"></div> <div class="postSeparator"></div> <div class="postTitle"> <a id="homepage1_HomePageDays_DaysList_ctl02_DayList_TitleUrl_1" class="postTitle2" href="http://www.cnblogs.com/xiandedanteng/p/7529033.html">Node.js 使用jQuery取得Nodejs http服务端返回的JSON数组示例</a> </div> <div class="postCon"><div class="c_b_p_desc">摘要: server.js代码: 页面代码:<a href="http://www.cnblogs.com/xiandedanteng/p/7529033.html" class="c_b_p_desc_readmore">阅读全文</a></div></div> <div class="clear"></div> <div class="postDesc">posted @ 2017-09-15 22:59 逆火狂飙 阅读(0) 评论(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=7529033" rel="nofollow">编辑</a></div> <div class="clear"></div> <div class="postSeparator"></div> <div class="postTitle"> <a id="homepage1_HomePageDays_DaysList_ctl02_DayList_TitleUrl_2" class="postTitle2" href="http://www.cnblogs.com/xiandedanteng/p/7528665.html">Node.js 使用jQuery取得Nodejs http服务端返回的JSON对象示例</a> </div> <div class="postCon"><div class="c_b_p_desc">摘要: server.js代码: 页面jQuery代码:<a href="http://www.cnblogs.com/xiandedanteng/p/7528665.html" class="c_b_p_desc_readmore">阅读全文</a></div></div> <div class="clear"></div> <div class="postDesc">posted @ 2017-09-15 21:54 逆火狂飙 阅读(0) 评论(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=7528665" rel="nofollow">编辑</a></div> <div class="clear"></div> <div class="postSeparator"></div> <div class="postTitle"> <a id="homepage1_HomePageDays_DaysList_ctl02_DayList_TitleUrl_3" class="postTitle2" href="http://www.cnblogs.com/xiandedanteng/p/7526966.html">Node.js 本地Xhr取得Node.js服务端数据的例子</a> </div> <div class="postCon"><div class="c_b_p_desc">摘要: 本以为用XHR取Nodejs http出的一段文字很简单,因为xhr取值和nodejs http出文字都是好弄的,谁知一试不是这回事,中间有个关键步骤需要实现。 nodejs http出文字显示在浏览器很容易,但是头信息是不完整的,下面resp.writeHead一句的红字部分就是这个关键步骤。 服<a href="http://www.cnblogs.com/xiandedanteng/p/7526966.html" class="c_b_p_desc_readmore">阅读全文</a></div></div> <div class="clear"></div> <div class="postDesc">posted @ 2017-09-15 16:09 逆火狂飙 阅读(4) 评论(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=7526966" rel="nofollow">编辑</a></div> <div class="clear"></div> <div class="postSeparator"></div> <div class="postTitle"> <a id="homepage1_HomePageDays_DaysList_ctl02_DayList_TitleUrl_4" class="postTitle2" href="http://www.cnblogs.com/xiandedanteng/p/7525888.html">Node.js node主文件找不到时报出的Error:Cannot find module异常</a> </div> <div class="postCon"><div class="c_b_p_desc">摘要: 如果执行>Node xx.js时,xx,js找不到的话,报出的错误是Error:Cannot find module 'c:/test/xx.js' 主文件名打错或者是点号打成逗号都会出这样的错。<a href="http://www.cnblogs.com/xiandedanteng/p/7525888.html" class="c_b_p_desc_readmore">阅读全文</a></div></div> <div class="clear"></div> <div class="postDesc">posted @ 2017-09-15 13:06 逆火狂飙 阅读(3) 评论(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=7525888" rel="nofollow">编辑</a></div> <div class="clear"></div> <div class="postSeparator"></div> <div class="postTitle"> <a id="homepage1_HomePageDays_DaysList_ctl02_DayList_TitleUrl_5" class="postTitle2" href="http://www.cnblogs.com/xiandedanteng/p/7525228.html">Node.js 极简入门Helloworld版服务器例子</a> </div> <div class="postCon"><div class="c_b_p_desc">摘要: 粗浅得很,纯属备忘。 效果是:<a href="http://www.cnblogs.com/xiandedanteng/p/7525228.html" class="c_b_p_desc_readmore">阅读全文</a></div></div> <div class="clear"></div> <div class="postDesc">posted @ 2017-09-15 10:56 逆火狂飙 阅读(2) 评论(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=7525228" rel="nofollow">编辑</a></div> <div class="clear"></div> </div> <div class="day"> <div class="dayTitle"> <a id="homepage1_HomePageDays_DaysList_ctl03_ImageLink" href="http://www.cnblogs.com/xiandedanteng/archive/2017/09/14.html">2017年9月14日</a> </div> <div class="postTitle"> <a id="homepage1_HomePageDays_DaysList_ctl03_DayList_TitleUrl_0" class="postTitle2" href="http://www.cnblogs.com/xiandedanteng/p/7522928.html">浅滩</a> </div> <div class="postCon"></div> <div class="clear"></div> <div class="postDesc">posted @ 2017-09-14 20:51 逆火狂飙 阅读(1) 评论(0) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=7522928" rel="nofollow">编辑</a></div> <div class="clear"></div> </div> <div class="topicListFooter"><div id="nav_next_page"><a href="http://www.cnblogs.com/xiandedanteng/default.html?page=2">下一页</a></div></div> </div><!--end: forFlow --> </div><!--end: mainContent 主体内容容器--> <div id="sideBar"> <div id="sideBarMain"> <div id="calendar"><div id="blog-calendar" style="display:none"></div><script type="text/javascript">loadBlogDefaultCalendar();</script></div> <div id="leftcontentcontainer"> <div id="blog-sidecolumn"></div><script type="text/javascript">loadBlogSideColumn();</script> </div> </div><!--end: sideBarMain --> </div><!--end: sideBar 侧边栏容器 --> <div class="clear"></div> </div><!--end: main --> <div class="clear"></div> <div id="footer"> <!--done--> Copyright ©2017 逆火狂飙 </div><!--end: footer --> </div><!--end: home 自定义的最大容器 --> </body> </html>
以上是关于Node.js 使用http客户端向网站请求数据并保存的主要内容,如果未能解决你的问题,请参考以下文章
无法使用javascript向ASP.NET web api Controller发送http POST请求