如何使用 phantomjs 从网站下载图像

2023-11-23

我想保存网站上的一些图像。目前我可以获得图像的路径,但我不知道如何使用 phantomJs 获取和保存图像。

findRotationTeaserImages = ->
  paths = page.evaluate ->
    jQuery('.rotate img').map(-> return this.src).get()

  for path, i in paths
    console.log(path);
    //save the image

我知道这是一个老问题,但是您只需将每个图像的尺寸和位置存储在对象中,然后更改 phantomjs page.clipRect 以便 page.render() 方法仅渲染其中的区域即可轻松完成此操作图像是。这是一个示例,从以下位置抓取多个图像http://dribbble.com/ :

var page = require('webpage').create();

page.open('http://dribbble.com/', function() {

    page.includeJs('//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js',function() {

        var images = page.evaluate(function() {
            var images = [];
            function getImgDimensions($i) {
                return {
                    top : $i.offset().top,
                    left : $i.offset().left,
                    width : $i.width(),
                    height : $i.height()
                }
            }
            $('.dribbble-img img').each(function() {
                var img = getImgDimensions($(this));
                images.push(img);
            });

            return images;
        });

        images.forEach(function(imageObj, index, array){
            page.clipRect = imageObj;
            page.render('images/'+index+'.png')
        });

        phantom.exit();
    });
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 phantomjs 从网站下载图像 的相关文章

随机推荐