我想循環遍歷每個li>,然後將它們添加到數組中。我應該如何去創建一個for循環來返回每個li並將它們放入數組中。在循環中獲取李是我卡住的地方。如何使用javascript在ul中循環播放li
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: false })
nightmare
.goto('https://en.wikipedia.org/wiki/Red_Hat_Enterprise_Linux')
.wait(500)
.evaluate(function() {
var ht = document.querySelector('#toc > ul > li.toclevel-1.tocsection-5 > ul
').innerText;
return ht;
})
.end()
.then(function (result) {
console.log(result)
})
.catch(function (error) {
console.error('Search failed:', error);
});
輸出:
PS C:\scrapping> node .\2.js
5.1 RHEL 2.1
5.2 RHEL 3
5.3 RHEL 4
5.4 RHEL 5
5.5 RHEL 6
5.6 RHEL 7
PS C:\scrapping>
感謝回答。 如果我這樣做,我得到這個: ''$ node stackOverflow.js {'0':{},'1':{},'2':{},'3':{},'4 ':{},'5':{}}'' 像空數組一樣。 –
這不會工作,由於我的回答 – devilpreet
@devilpreet中提到的原因謝謝!正是我想要的。 –