[英]extracting html element with cherrio
該流星服務器代碼使用了cheerio並嘗試提取文本“ John”,我嘗試了以下幾種不同的方法,但無濟於事。
console.log(e.next.children.eq(1).text()); console.log(e.next.children.last().text()); console.log(e.next.contents().last().text());
如何用cheerio完成? 謝謝
ResObj.$("table").contents().filter(function() {
return this.nodeType == 8;
}).each(function(i, e) {
if (e.nodeValue.trim() == 'CONTACTS') {
console.log(e.next.contents('td').eq(1).text()); //<----------
console.log(e.nextSibling.nodeType); // returns 3
}
});
<!-- CONTACTS -->
<tr>
<td class="label" valign="top" align="right" style="white-space:nowrap">
Names of people:
</td>
<td class="displayValue" valign="top">
John
</td>
</tr>
Cheerio就像jQuery(在大多數情況下)一樣,所以為什么要構建這種復雜的結構,而看起來卻只需要:
$('table .displayValue').each(function () {
console.log($(this).text());
});
讓我們分解一下您擁有的代碼:
// Get all tables contents, then filter the data
ResObj.$("table").contents().filter(function() {
// Only return Comments? https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType ... also you should be using ===
return this.nodeType == 8;
// Loop through all of the filtered elements
}).each(function(i, e) {
// Check the string of the comment for the text you want --- This is bad style
if (e.nodeValue.trim() == 'CONTACTS') {
console.log(e.next.contents('td').eq(1).text()); //<----------
console.log(e.nextSibling.nodeType); // returns 3
}
});
那么,為什么要遍歷所有這些循環,只是為了獲得一個具有易於訪問的類的選擇器,是他們的原因嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.