Node.js 使用http客戶端向網站請求資料並儲存

weixin_33697898發表於2017-09-17

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&nbsp;
文章-0&nbsp;
評論-111&nbsp;
</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(&quot;http&quot;); var fs=require(&quot;fs&quot;); var _dirname=&quot;./html&quot; // 建立伺服器,建立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">摘要: 如果執行&gt;Node xx.js時,xx,js找不到的話,報出的錯誤是Error:Cannot find module &#39;c:/test/xx.js&#39; 主檔名打錯或者是點號打成逗號都會出這樣的錯。<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 &copy;2017 逆火狂飆
    </div><!--end: footer -->
</div><!--end: home 自定義的最大容器 -->
</body>
</html>

 

相關文章