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就可以访问了, ...
随机推荐
- hdu 1534(差分约束)
Schedule Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext读取配置文件的方法
先说:ClassPathXmlApplicationContext 这个类,默认获取的是WEB-INF/classes/下的路径,也就是在myeclipse的src下的路径,所以用这个是获取不到WEB ...
- 微信小程序开发参考资料汇总
不错的微信小程序入门教程:微信小程序入门二: 条件.遍历.网络请求.获取本地图片http://blog.csdn.net/lecepin/article/details/54016701 微信小程序入 ...
- Mysql优化之——启用查询缓存
启用MySQL查询缓存可以极大地减低数据库服务器的CPU使用率,实际使用情况是:开启前CPU使用率120%左右,开启后降到了10%. 查看查询缓存情况: mysql> show variable ...
- AC日记——由乃与大母神原型和偶像崇拜 洛谷 P3792
由乃与大母神原型和偶像崇拜 思路: 逆元+线段树维护和+线段树维护平方和+线段树维护最大最小值: 代码: #include <bits/stdc++.h> using namespace ...
- javascript+dom编程艺术 读后感
利用上班空闲2,3天把这本书看完了,整体来说,这本书很不错.虽然我js有一定的基础了,jquery基础也会使用,但是我觉得对js应该有个循序渐进的理解,所以还是把js系统的学习一遍.我看技术类的书总数 ...
- xunsearch如何按照ID排序
你ini再建一个字段id_tmp 类型type=numeric 重建索引的时候 数据源 加一个主键id的别名 id, id as id_tmp 排序的时候按照id_tmp排序
- scrapy 最新版本中文文档地址
http://scrapy-chs.readthedocs.org/zh_CN/latest/
- npm命令要记
npm list - depth 0 查看依赖 cnpm install 安装 npm outdated 查看模块过时 npm cache clear
- poj1860(Bellman—fold)
题目连接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our ...