Ran*_*ndy 4 node.js selectors-api jsdom
我正在尝试使用我的应用程序从维基百科页面中抓取一些信息Node.js,使用jsdom. 这是我正在做的一个例子:
jsdom.env({
url: "https://en.wikipedia.org/wiki/Bill_Gates",
features: {
FetchExternalResources: ['script'],
ProcessExternalResources: ['script'],
SkipExternalResources: false,
},
done: function (err, window) {
if (err) {
console.log("Error: ", err)
return;
}
var paras = window.document.querySelectorAll('p');
console.log("Paras: ", paras)
}
});
Run Code Online (Sandbox Code Playgroud)
奇怪的是,它querySelectorAll('p')返回一个NodeList空元素:
Paras: NodeList {
'0': HTMLParagraphElement {},
'1': HTMLParagraphElement {},
'2': HTMLParagraphElement {},
'3': HTMLParagraphElement {},
'4': HTMLParagraphElement {},
'5': HTMLParagraphElement {},
'6': HTMLParagraphElement {},
'7': HTMLParagraphElement {},
...
62': HTMLParagraphElement {} }
Run Code Online (Sandbox Code Playgroud)
知道可能是什么问题吗?谢谢!
编辑:
window.document.querySelectorAll('p')替换为时我得到了相同的结果window.document.getElementsByTagName('p')
这些元素不是空的,它只是不会在控制台日志中显示结果。您必须访问它们的数据(textContent例如)
尝试这个:
Array.prototype.slice.call(dom.window.document.getElementsByTagName("p")).map(p => {
console.log(p.textContent);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3626 次 |
| 最近记录: |