Javascript 是否可以在map函数的return语句中检查DOM元素的NULL?
我有一个小的网页抓取scipt运行,它适用于大多数项目,直到网站上的模式发生变化 网站被删除(遵守使用条款和robots.txt): 这是一个德语页面,上面有学生对大学/课程的评论 在下面的代码中,我获取了一个完整的div容器,其中包含每个学生评级所需的所有内容,并在puppeters页面求值函数的return语句中为map函数中的构造函数将其拆分 建造商:Javascript 是否可以在map函数的return语句中检查DOM元素的NULL?,javascript,node.js,puppeteer,Javascript,Node.js,Puppeteer,我有一个小的网页抓取scipt运行,它适用于大多数项目,直到网站上的模式发生变化 网站被删除(遵守使用条款和robots.txt): 这是一个德语页面,上面有学生对大学/课程的评论 在下面的代码中,我获取了一个完整的div容器,其中包含每个学生评级所需的所有内容,并在puppeters页面求值函数的return语句中为map函数中的构造函数将其拆分 建造商: 功能语料库(课程、评分、作者、文本、日期){ 本课程=课程; 这个。评级=评级; this.author=作者; this.text=
功能语料库(课程、评分、作者、文本、日期){
本课程=课程;
这个。评级=评级;
this.author=作者;
this.text=文本;
this.date=日期;
}
不是说这是最干净的解决方案,而是为了解决您的问题:
这:
相当于:
return reviewBlock.map(block => {
// So you can make your if conditions here,
// and build the array dynamically before returning it.
return [
block.querySelector(".item-course-title a").textContent.trim(),
block.querySelector("div .rating-value").textContent.trim(),
block.querySelector(".item-author").innerText,
block.querySelector("p.item-text").innerText.trim(),
block.querySelector('span.item-date').innerText
]
});
return reviewBlock.map(block => {
// So you can make your if conditions here,
// and build the array dynamically before returning it.
return [
block.querySelector(".item-course-title a").textContent.trim(),
block.querySelector("div .rating-value").textContent.trim(),
block.querySelector(".item-author").innerText,
block.querySelector("p.item-text").innerText.trim(),
block.querySelector('span.item-date').innerText
]
});