Node.js 访问https网站
源码:
//==================================================== // 访问https://www.zhihu.com/得到pagecode // 2017年11月6日 //==================================================== // 内置https模块,提供了https服务器和客户端功能 var https=require("https"); // cheerio模块,提供了类似jQuery的功能 var cheerio = require("cheerio"); // 内置文件处理模块 var fs=require('fs'); // 请求参数JSON var options; // request请求 var req; //-------------------------------------- // 程序入口 //-------------------------------------- function start(){ // 初始化options options={ hostname:'www.zhihu.com', port:443, path:'/',// 子路径 method:'GET', agent:false, }; req=https.request(options,function(resp){ resp.setEncoding('utf8'); var body=""; resp.on('data',function(chunk){ body+=chunk; }); resp.on('end',function(){ var $ = cheerio.load(body); console.log(body); }); }); // 超时处理 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(); } // 调用start函数,程序开始 start();
得到的body数据:
<!DOCTYPE html> <html lang="zh-CN" class=""> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> <meta http-equiv="X-ZA-Response-Id" content="110f3b166e9849aa"> <meta http-equiv="X-ZA-Experiment" content="default:None,ge3:ge3_9,ge2:ge2_1,nweb_sticky_sidebar:sticky,live_review_buy_bar:live_review_buy_bar_2,is_office:false,home_ui2:default,is_show_unicom_free_entry:unicom_free_entry_off,app_store_rate_dialog:close,qa_sticky_sidebar:sticky_sidebar,android_profile_panel:panel_b,live_store:ls_a2_b2_c1_f2,search_hybrid_tabs:without-tabs,answer_related_readings:qa_recommend_with_ads_and_article,asdfadsf:asdfad,new_mobile_column_appheader:new_header,fav_act:default,remix_one_key_play_button:headerButton,mobile_qa_page_proxy_heifetz:m_qa_page_nweb,nweb_write_answer:default,android_pass_through_push:getui,new_more:new,new_buy_bar:livenewbuy3,zcm-lighting:zcm,iOS_newest_version:4.2.0,qrcode_login:qrcode,wechat_share_modal:wechat_share_modal_show"> <meta name="renderer" content="webkit" /> <meta name="description" content="中文互联网最大的知识平台,帮助人们便捷地分享彼此的知识、经验和见解。"/> <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"/> <title>知乎 - 发现更大的世界</title> <link rel="apple-touch-icon" href="https://static.zhihu.com/static/revved/img/ios/touch-icon-152.87c020b9.png" sizes="152x152"> <link rel="apple-touch-icon" href="https://static.zhihu.com/static/revved/img/ios/touch-icon-120.496c913b.png" sizes="120x120"> <link rel="apple-touch-icon" href="https://static.zhihu.com/static/revved/img/ios/touch-icon-76.dcf79352.png" sizes="76x76"> <link rel="apple-touch-icon" href="https://static.zhihu.com/static/revved/img/ios/touch-icon-60.9911cffb.png" sizes="60x60"> <link rel="shortcut icon" href="https://static.zhihu.com/static/favicon.ico" type="image/x-icon" /> <link rel="dns-prefetch" href="p1.zhimg.com"/> <link rel="dns-prefetch" href="p2.zhimg.com"/> <link rel="dns-prefetch" href="p3.zhimg.com"/> <link rel="dns-prefetch" href="p4.zhimg.com"/> <link rel="dns-prefetch" href="comet.zhihu.com"/> <link rel="dns-prefetch" href="static.zhihu.com"/> <link rel="dns-prefetch" href="upload.zhihu.com"/> <link rel="stylesheet" href="https://static.zhihu.com/static/revved/-/css/pages/unlogin-index/main.f214513a.css"> <meta name="google-site-verification" content="FTeR0c8arOPKh8c5DYh_9uu98_zJbaWw53J-Sch9MTg" /> <meta name="baidu-site-verification" content="KPFppAFoYF4Kkdv9" /> <meta property="qc:admins" content="00544670776201056375" /> <link rel="canonical" href="http://www.zhihu.com" /> <meta id="znonce" name="znonce" content="7b6bfcce1dba4927bc2fde931b3037fd"> <!--[if lt IE 9]> <script src="https://static.zhihu.com/static/components/respond/dest/respond.min.js"></script> <link href="https://static.zhihu.com/static/components/respond/cross-domain/respond-proxy.html" id="respond-proxy" rel="respond-proxy" /> <link href="/static/components/respond/cross-domain/respond.proxy.gif" id="respond-redirect" rel="respond-redirect" /> <script src="/static/components/respond/cross-domain/respond.proxy.js"></script> <![endif]--> <script src="https://static.zhihu.com/static/revved/-/js/instant.14757a4a.js"></script> </head> <body class="zhi "> <div class="index-main"> <div class="index-main-body"> <div class="index-header"> <h1 class="logo hide-text">知乎</h1> <h2 class="subtitle">与世界分享你的知识、经验和见解</h2> </div> <div class="desk-front sign-flow sign-flow clearfix sign-flow-simple"> <div class="index-tab-navs"> <div class="navs-slider"> <a href="#signup" class="active">注册</a> <a href="#signin">登录</a> <span class="navs-slider-bar"></span> </div> </div> <div class="view view-signin" data-za-module="SignInForm"> <form method="POST"> <input type="hidden" name="_xsrf" value="c8581655229e04f445154e91134bc9c7"/> <div class="group-inputs"> <div class="account input-wrapper"> <input type="text" name="account" aria-label="手机号或邮箱" placeholder="手机号或邮箱" required> </div> <div class="verification input-wrapper"> <input type="password" name="password" aria-label="密码" placeholder="密码" required /><button type="button" class="send-code-button">获取验证码</button> </div> <div class="Captcha input-wrapper" data-type="cn" data-za-module="Captcha"> <div class="Captcha-operate"> <input type="hidden" name="captcha" required data-rule-required="true" data-msg-required="请点击图中所有倒立的文字"> <input type="hidden" name="captcha_type" value="cn" required> <label class="Captcha-prompt">请点击图中所有倒立的文字</label> <span class="Captcha-refresh js-refreshCaptcha sprite-index-icon-refresh"></span> </div> <div class="Captcha-imageConatiner"> <img class="Captcha-image" alt="验证码" > </div> </div> </div> <div class="button-wrapper command"> <button class="sign-button submit" type="submit">登录</button> </div> <div class="signin-misc-wrapper clearfix"> <button type="button" class="signin-switch-button">手机验证码登录</button> <a class="unable-login" href="#">无法登录?</a> </div> <div class="other-signup-wrapper" data-za-module="SNSSignIn"> <span class="name signin-switch-qrcode-buttons">二维码登录</span> <span class="signup-footer-separate signup-footer-se"> · </span> <span class="name signup-social-buttons js-toggle-sns-buttons">社交帐号登录</span> <div class="sns-buttons"> <a title="微信登录" class="js-bindwechat" href="#"><i class="sprite-index-icon-wechat"></i></a> <a title="微博登录" class="js-bindweibo" href="#"><i class="sprite-index-icon-weibo"></i></a> <a title="QQ 登录" class="js-bindqq" href="#"><i class="sprite-index-icon-qq"></i></a> </div> </div> </form> <div class="qrcode-signin-container"> <div class="qrcode-signin-step1"> <div class="qrcode-signin-img-wrapper"> <img src="/static/img/spinner/grey-loading.gif" class="qrcode-signin-loading"/> </div> <p>打开最新 <a href="https://www.zhihu.com/app/" target="_blank">知乎 App</a></p> <p>在「更多」页面右上角打开扫一扫</p> <div class="qrcode-signin-cut-button"> <span class="signin-switch-password">使用密码登录</span> </div> </div> <div class="qrcode-signin-step2"> <div class="qrcode-signin-scan-status"></div> <p class="qrcode-signin-scan-tips">扫描成功</p> <p>请在手机上「确认登录」</p> <div class="qrcode-signin-cut-button"> <span class="qrcode-goto-scan">返回二维码</span> </div> </div> <div class="qrcode-signin-failure"> <div class="qrcode-signin-failure-icon"></div> <p class="qrcode-signin-failure-message"></p> <div class="qrcode-signin-cut-button"> <span class="signin-switch-password">使用密码登录</span> </div> </div> <div class="qrcode-signin-guide"></div> </div> <div class="QRCode"> <button class="QRCode-toggleButton"> <span class="sprite-global-icon-qrcode"></span> <span class="QRCode-toggleButtonText ">下载知乎 App</span> </button> <div class="QRCode-card"> <div class="QRCode-image"></div> <div class="sprite-index-icon-arrow"></div> </div> </div> </div> <div class="view view-signup selected" data-za-module="SignUpForm"> <form class="zu-side-login-box" action="/register/email" id="sign-form-1" autocomplete="off" method="POST"> <input type="password" hidden> <input type="hidden" name="_xsrf" value="c8581655229e04f445154e91134bc9c7"/> <div class="group-inputs"> <div class="name input-wrapper"> <input required type="text" name="fullname" aria-label="姓名" placeholder="姓名"> </div> <div class="email input-wrapper"> <input required type="text" class="account" name="phone_num" aria-label="手机号" placeholder="手机号"> </div> <div class="input-wrapper"> <input required type="password" name="password" aria-label="密码" placeholder="密码(不少于 6 位)" autocomplete="off"> </div> <div class="Captcha input-wrapper" data-type="cn" data-za-module="Captcha"> <div class="Captcha-operate"> <input type="hidden" name="captcha" required data-rule-required="true" data-msg-required="请点击图中所有倒立的文字"> <input type="hidden" name="captcha_type" value="cn" required> <label class="Captcha-prompt">请点击图中所有倒立的文字</label> <span class="Captcha-refresh js-refreshCaptcha sprite-index-icon-refresh"></span> </div> <div class="Captcha-imageConatiner"> <img class="Captcha-image" alt="验证码" > </div> </div> </div> <div class="button-wrapper command"> <button class="sign-button submit" type="submit">注册知乎</button> </div> </form> <p class="agreement-tip">点击「注册」按钮,即代表你同意<a href="/terms" target="_blank">《知乎协议》</a></p> <a class="signup-entry--org" href="/org/signup">注册机构号</a> <div class="QRCode"> <button class="QRCode-toggleButton"> <span class="sprite-global-icon-qrcode"></span> <span class="QRCode-toggleButtonText ">下载知乎 App</span> </button> <div class="QRCode-card"> <div class="QRCode-image"></div> <div class="sprite-index-icon-arrow"></div> </div> </div> </div> </div> </div> </div> <div class="footer"> <a target="_blank" href="https://zhuanlan.zhihu.com">知乎专栏</a> <span class="dot">·</span> <a target="_blank" href="/roundtable">知乎圆桌</a> <span class="dot">·</span> <a target="_blank" href="/explore" data-za-c="explore" data-za-a="visit_explore" data-za-l="home_bottom_explore">发现</a> <span class="dot">·</span> <a target="_blank" href="/app">移动应用</a> <span class="dot">·</span> <a href="/contact" class="footer-mobile-show">联系我们</a> <span class="dot">·</span> <a target="_blank" href="/careers">来知乎工作</a> <br /> <span>© 2017 知乎</span> <span class="dot">·</span> <a href="http://www.miibeian.gov.cn/" target="_blank">京 ICP 证 110745 号</a> <span class="dot">·</span> <span>京公网安备 11010802010035 号</span> <span class="dot">·</span> <a href="http://zhstatic.zhihu.com/assets/zhihu/publish-license.jpg" target="_blank">出版物经营许可证</a> <br /> <a target="_blank" href="https://zhuanlan.zhihu.com/p/28852607">侵权举报</a> <span class="dot">·</span> <a target="_blank" href="http://www.12377.cn">网上有害信息举报专区</a> <span class="dot">·</span> <a target="_blank" href="/jubao">儿童色情信息举报专区</a> <span class="dot">·</span> <span>违法和不良信息举报:010-82716601</span> <div class="chengxing"> <a id='___szfw_logo___' href='https://credit.szfw.org/CX20170607038331320388.html' target='_blank'> <img src="https://static.zhihu.com/static/revved/img/index/chengxing_logo@2x.65dc76e8.png" border='0' /> </a> <script type='text/javascript'>(function(){document.getElementById('___szfw_logo___').oncontextmenu = function(){return false;}})();</script> </div> </div> <script type="text/json" class="json-inline" data-name="disabled_components">["back_to_top"]</script> ,0]</script> <script type="text/json" class="json-inline" data-name="env">["zhihu.com","comet.zhihu.com",false,null,false,false]</script> ,"now":1509916984000,"abtest_mask":"------------------------------","user_attr":[0,0,0,"-","-"],"user_hash":0}</script> <script src="https://static.zhihu.com/static/revved/-/js/vendor.cb14a042.js"></script> <script src="https://static.zhihu.com/static/revved/-/js/closure/base.41bb3b24.js"></script> <script src="https://static.zhihu.com/static/revved/-/js/closure/common.ef6c9c27.js"></script> <script src="https://static.zhihu.com/static/revved/-/js/closure/page-index.f17f3a40.js"></script> <meta name="entry" content="ZH.entrySignPage" data-module-id="page-index"> <input type="hidden" name="_xsrf" value="c8581655229e04f445154e91134bc9c7"/> </body> </html>
Node.js 访问https网站的更多相关文章
- Nodejs学习笔记(十五)--- Node.js + Koa2 构建网站简单示例
目录 前言 搭建项目及其它准备工作 创建数据库 创建Koa2项目 安装项目其它需要包 清除冗余文件并重新规划项目目录 配置文件 规划示例路由,并新建相关文件 实现数据访问和业务逻辑相关方法 编写mys ...
- Nginx 配置HTTPS 与Node.js 配置HTTPS方法
前段时间公司网站要求加上HTTPS安全CA证书,公司服务器全是阿里云服务器,并且配有负载均衡,所以选择直接在阿里云购买CA证书,阿里云有一种证书可以免费试用一年,决定申请此证书,阿里云证书需要验证,阿 ...
- [转]Nodejs学习笔记(十五)--- Node.js + Koa2 构建网站简单示例
本文转自:https://www.cnblogs.com/zhongweiv/p/nodejs_koa2_webapp.html 目录 前言 搭建项目及其它准备工作 创建数据库 创建Koa2项目 安装 ...
- Nodejs学习笔记(十五)—Node.js + Koa2 构建网站简单示例
前言 前面一有写到一篇Node.js+Express构建网站简单示例:http://www.cnblogs.com/zhongweiv/p/nodejs_express_webapp.html 这篇还 ...
- 使用Node.JS访问Hyperledger Fabric的gRPC服务
在即将正式发布的Hyperledger Fabric SDK 1.0中,Hyperledger Fabric通过gRPC提供服务接口以取代现有的REST API.本文介绍了如何使用Node.JS访问H ...
- AFNetworking 原作者都无法解决的问题: 如何使用ip直接访问https网站?
背景 最近App似乎有报异常是DNS无法解析,尝试解决此问题.搜集到的资料很少,甚至连AFN原作者都判定这可能是一个无解的问题,参见: https://github.com/AFNetworking/ ...
- Python使用requests模块访问HTTPS网站报错`certificate verify failed`
使用requests模块访问HTTPS网站报错: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Nam ...
- c# 中HttpClient访问Https网站
c# 中HttpClient访问Https网站,加入如下代码: handler = new HttpClientHandler() ;handler.AllowAutoRedirect = true; ...
- burp 代理的时候无法访问https网站
今天在使用burp的时候发现不能访问https网站了,Google下面还出现这个 ERR_SSL_VERSION_OR_CIPHER_MISMATCH,于是到官网下载了一个最新的burp就可以访问了, ...
随机推荐
- git常用命令收集
[git远程操作命令] 1.$ git remote –v #查看本地配置的所有远程仓库,内容为远程仓库的地址和本地别名 harvey@harvey:~/node$ git remote -v nod ...
- Highcharts制作图片表设置线条颜色和粗细
Chart:图表区选项 Chart图表区选项用于设置图表区相关属性. 参数 描述 默认值 backgroundColor 设置图表区背景色 #FFFFFF borderWidth 设置图表边框宽度 0 ...
- python encode和decode函数说明
字符串编码常用类型:utf-8,gb2312,cp936,gbk等. Python中,我们使用decode()和encode()来进行解码和编码 在python中,使用unicode类型作为编码的基础 ...
- tushare 安装
1.pip install lxml 2.pip install pandas 3.pip install requests 4.pip install lxml 5.pip install tush ...
- 【转载】Linux kill, killall, kill -9
1) 查看进程的方法: ps -ef 或者 ps aux root 15087 0.0 0.0 0 0 ? S 23:31 0:00 [kwo ...
- 最小生成树的Prim算法
构造最小生成树的Prim算法 假设G=(V,E)为一连通网,其中V为网中所有顶点的集合,E为网中所有带权边的集合.设置两个新的集合U和T,其中集合U用于存放G的最小生成树的顶点,集合T用于 ...
- HDU 3342 拓扑排序模板
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 高效mysql的习惯(程序员版本)
高效的mysql 一定要有主键 如果没有主键: 数据多次读写后可能更离散,有更多随机I/O MySQL复制环境中,如果选择RBR模式,没有主键的update需要读全表,导致复制延迟 好的主键特点: 没 ...
- 集训考试题(CF510C Fox And Names的简化版)
题目描述给定n个由小写字母组成的字符串,请你求出一个字母表顺序,使得这n个字符串是按照字典序升序排列的,数据保证存在合法的字母表顺序.如果存在多个解,输出字典序最小的那个. 输入格式第一行一个整数n. ...
- 线段树【p4879】ycz的妹子
Description 机房神犇yczycz有n个青梅竹马,她们分别住在1~n号城市中.小时候的她们美丽可爱,但是由于女大十八变,有些妹子的颜值发生了变化,但是十分重感情的\(ycz\)神犇不忍心抛弃 ...