我正在尝试使用无浏览器获取HTML页面中所有img标记的src值。我目前的实现如下所示:
async function run() {
const chromeless = new Chromeless();
let url = 'http://someurl/somepath.html';
var allImgUrls = await chromeless
.goto(url)
.evaluate(() => document.getElementsByTagName('img'));
var htmlContent = await chromeless
.goto(url)
.evaluate(() => document.documentElement.outerHTML );
console.log(allImgUrls);
await chromeless.end()
}问题是,我没有在allImgUrls中获得img对象的任何值。
发布于 2018-06-12 05:33:00
经过一些研究,我们发现我们可以采用这样的方法:
var imgSrcs = await chromeless
.goto(url)
.evaluate(() => {
/// since document.querySelectorAll doesn't actually return an array but a Nodelist (similar to array)
/// we call the map function from Array.prototype which is equivalent to [].map.call()
const srcs = [].map.call(document.querySelectorAll('img'), img => img.src);
return JSON.stringify(srcs);
});https://stackoverflow.com/questions/50793954
复制相似问题