![](/img/trans.png)
[英]How to scrape a table with changing data using Cheerio in Node.js?
[英]How to retrieve all texts, including 'non-between' html tags using cheerio on node.js?
请考虑以下HTML内容:
I like <div>turtles, </div><b> cookies</b> and brownies.
我正在使用下面的代码来检索内容:
txt=$.root().find('*').contents().filter(function() {
if (this.type === 'text'){
varArr.push(this.data);
return this.data
}
}).text();
txt是:乌龟,饼干
如果我添加标签
<html>I like <div>turtles, </div><b> cookies</b> and brownies</html>
我得到:我喜欢和布朗尼蛋糕。 乌龟,饼干
我想要:我喜欢乌龟,饼干和布朗尼蛋糕。
它不选择初始和最后文本,不在标签之间。 有没有办法检索所有文本-在外部并嵌套在html标签上?)
我最终对原始字符串进行了转换,以检索所需的值。 我正在检查字符串是否以html标签开头或结尾。 如果没有,我添加了一个tmp标签。 即:
<tmp>I like </tmp><div>turtles, </div><b> cookies</b> and </tmp>brownies</tmp>
此后,我删除了tmp标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.