[英]How to get childNodes of a div in cheerio?
I want to get the first childNode of a div using cheerio.我想使用cheerio获取div的第一个childNode。 I am getting it using javascript dom manipulation.
我正在使用 javascript dom 操作来获取它。 but can't get it on cheerio.
但不能在cheerio上得到它。
I have already tried it in dev tool and I got the expected result.我已经在开发工具中尝试过了,我得到了预期的结果。 but I want it by using cheerio.
但我想要它使用cheerio。
javascript javascript
document.querySelector('.title_wrapper .subtext').childNodes[0].textContent;
I want to get the text 'PG' from this element.我想从此元素中获取文本“PG”。
<div class="subtext">
PG
<span class="ghost">|</span>
<time datetime="PT121M">
2h 1min
</time>
<span class="ghost">|</span>
<a href="/search/title?genres=action&explore=title_type,genres&ref_=tt_ov_inf">Action</a>,
<a href="/search/title?genres=adventure&explore=title_type,genres&ref_=tt_ov_inf">Adventure</a>,
<a href="/search/title?genres=fantasy&explore=title_type,genres&ref_=tt_ov_inf">Fantasy</a>
<span class="ghost">|</span>
<a href="/title/tt0076759/releaseinfo?ref_=tt_ov_inf" title="See more release dates">25 May 1977 (USA)</a>
</div>
你几乎拥有它,只需使用 [0] 来获取 javascript 节点:
$('.subtext')[0].childNodes[0].nodeValue.trim()
On your specific situation, this is how to fetch data and you can apply to a massive bulk of data to your extraction:根据您的具体情况,这是获取数据的方法,您可以将大量数据应用于提取:
var fullText = $('.subtext').text();
// Returns:
// PG|2h 1min|Action,Adventure,Fantasy|25 May 1977 (USA)
var arrSplit = fullText.split('|');
// Splits by ('|') pipe character into an Array
// [ 'PG', '2h 1min', 'Action,Adventure,Fantasy', '25 May 1977 (USA)' ]
var firstChildNode = arrSplit[0];
// Gets the "first" childNode of this specific situation
// PG
You can clone the parent and then remove all the child elements, leaving only the text for you to select.您可以克隆父元素,然后删除所有子元素,只留下文本供您选择。
$(".title_wrapper .subtext")
.clone() //clone the element
.children() //select all children
.remove() //remove all children
.end() //go back to selected element
.text(); //get the text of element
This is an old jQuery solution .这是一个旧的jQuery 解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.