javascript - 如何查询cheerio以获取包含选择器标签的html输出
问题描述
如果您使用$("body").html()
cheerio,则返回body 标签内的html。如何查询 Cheerio 的body
包含开始和结束body
标签的内容?
以这样的文件为例:
<body class="body">
<div>
<h1>Foo Bar</h1>
</div>
</body>
<style>
.body {
margin: 0;
}
</style>
以下代码将html
输出body
. 输出将不包含<body>
标签,这是我想要实现的
const cheerio = require("cheerio");
const $ = cheerio.load(text);
const body = $("body").html();
console.log(body);
// <div>
// <h1>Foo Bar</h1>
// </div>
解决方案
使用$.html("body")
而不是$("body").html()
.
推荐阅读
- android - ANDROID VOLLEY + JWT TOKEN 认证 + DJANGO REST 框架
- node.js - 不能将扩展运算符与三元运算符一起使用
- tensorflow - 将 .pb 文件转换为 .tflite 的工作流程
- api-platform.com - 根据经过身份验证的用户自动过滤属性
- python - 如何在基于点的游戏中跟踪点?在 Python 中
- api - 仅当用户等于经过身份验证的用户时,如何使用规范化组获取用户电子邮件
- r - 未找到 R 版本 4.0 的 D3heatmap
- javascript - 如何返回其键作为函数参数提供的对象值?
- c# - 使用 C# UWP 运行另一个程序
- python - 在gym-retro(google colab)中导入ROM时语法无效