【问题标题】:Chromeless - get all images src from a webpageChromeless - 从网页获取所有图像 src
【发布时间】:2018-06-12 13:33:00
【问题描述】:

我正在尝试使用 Chromeless 获取 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 对象的值。

【问题讨论】:

    标签: image html-parsing chromeless


    【解决方案1】:

    经过一番研究,发现我们可以使用这种方法:

    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);
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多