前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >cheerio获取outerHTML

cheerio获取outerHTML

作者头像
全栈程序员站长
发布2022-06-28 13:44:31
1.3K0
发布2022-06-28 13:44:31
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

cheerio作为node中jquery的替代品,拥有与jquery相似的api,甚至连详细文档的地址都指向api.jquery.com。但是由于执行环境的关系,并没有完全继承jquery中的方法。 对于这样的页面

代码语言:javascript
复制
<html>
	<head></head>
	<body>
		<ul id="fruits">
			<li>1</li>
			<li>2</li>
		</ul>
		<ul id="others">
			<li>1</li>
			<li>2</li>
		</ul>
	</body>
</html>

在浏览器中,使用jquery获取所选取对象的包括本身标签的内容时,会用到下面的方法 ("......").prop("outerHTML") 例如若要去取id等于fruits的内容

但是这在cheerio中行不通。 网上搜索了一圈后基本都是一套翻译完的文档无限转载。。。还是自己动手写了两个方法。

方法一

代码语言:javascript
复制
var cheerio = require('cheerio');

const $ = cheerio.load('<html><head></head><body><ul id="fruits"><li>1</li><li>2</li></ul><ul id="others"><li>1</li><li>2</li></ul></body></html>');

console.log(cheerio.load('<div></div>')("div").html($("#fruits")).html());

既然它只能获取内容,那就造一个容器把它包进去再取。就是普通的jquery语法不解释。

方法二

改源码 核心的文件有两个。分别是cheerio包下的manipulation.js

代码语言:javascript
复制
exports.html = function(str) { 
   
  if (str === undefined) { 
   
    if (!this[0] || !this[0].children) return null;
    return $.html(this[0].children, this.options);
  }

  var opts = this.options;

  domEach(this, function(i, el) { 
   
    _.forEach(el.children, function(child) { 
   
      child.next = child.prev = child.parent = null;
    });

    var content = str.cheerio ? str.clone().get() : evaluate('' + str, opts, false);

    updateDOM(content, el);
  });

  return this;
};

还有static.js

代码语言:javascript
复制
exports.html = function(dom, options) { 
   

  // be flexible about parameters, sometimes we call html(),
  // with options as only parameter
  // check dom argument for dom element specific properties
  // assume there is no 'length' or 'type' properties in the options object
  if (Object.prototype.toString.call(dom) === '[object Object]' && !options && !('length' in dom) && !('type' in dom))
  { 
   
    options = dom;
    dom = undefined;
  }

  // sometimes $.html() used without preloading html
  // so fallback non existing options to the default ones
  options = _.defaults(flattenOptions(options || { 
   }), this._options, defaultOptions);

  return render(this, dom, options);
};

虽然完全搞不懂nodejs是怎么运行的(纯靠报错和ctrl+f硬找,我自己都意外的是在用断点之前就找到了解决方法),总之,在manipulation.js中添加这段代码

代码语言:javascript
复制
exports.outerHTML = function(str) { 
   
    return $.html(this[0], this.options);
}

然后这样调用也是可以的

代码语言:javascript
复制
var cheerio = require('cheerio');

const $ = cheerio.load('<html><head></head><body><ul id="fruits"><li>1</li><li>2</li></ul><ul id="others"><li>1</li><li>2</li></ul></body></html>');

console.log($("#fruits").outerHTML());

但是,这可能不符合规范,先用方法一凑合着吧。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/150580.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 方法一
  • 方法二
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档