使用casperjs下载资源图像文件

时间:2017-08-01 08:57:18

标签: javascript node.js phantomjs casperjs slimerjs

我阅读了这些文档,看起来您需要使用slimerjs http://docs.casperjs.org/en/latest/events-filters.html才能从package main import ( "context" "fmt" "time" ) func interruptable_call(sleep time.Duration) <-chan time.Time { fmt.Println("sleeping for ", sleep*time.Second) time.Sleep(sleep * time.Second) return time.After(0 * time.Second) } func A(ctx context.Context) int { for { select { case <-ctx.Done(): fmt.Println("func done") return 1 case <-interruptable_call(2): fmt.Println("blocking") case <-interruptable_call(3): fmt.Println("blocking") case <-interruptable_call(4): fmt.Println("blocking") case <-interruptable_call(5): fmt.Println("blocking") } } return 0 } func main() { ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() fmt.Println("go A") go A(ctx) fmt.Println("go A done") select { case <-ctx.Done(): fmt.Println("main done") break } } 事件中获取responseData.body

我的用例是在页面加载时下载图像,所以我不做另外的往返:获取资源JSON,下载并保存任何图像文件,重新加载文件以检查图像尺寸和如果太小(图标) - &gt;消除。

我想知道是否有更好的方法来做到这一点。事实上,我可以去page.resource.received evaluate选择器,但有些网站会使用img css,这很棘手。

1 个答案:

答案 0 :(得分:0)

评估可能很棘手,但这是一种可行的方法:( PhantomJS Only

这个例子有可能

  1. 存储符合特定条件的clipRects图像 这些元素的后page.render()到屏幕截图文件中。

  2. 存储匹配资源的URL以供后续下载 请求

  3. 捕获内部网址&#39; src&#39;属性或背景图像&#39; css属性,试图获得宽度&amp;标准匹配和捕获目的的高度。

  4. var page = require('webpage').create();
    page.onConsoleMessage = function(msg) {console.log(msg);};
    console.log('[#] I M A G E · N I N J A');
    page.open('http://cartawifi.com', function(status) {
      var clipRectList = page.evaluate(function(pagex) {
        // .: Captured Images : In-Memory Reference Storage :.
        const IMAGES = {
            'src':{'indxs':[],'ref':[]},
            'background-image':{'indxs':[],'ref':[]},
            'selectors':[]
        }; 
        var clipRects = []; // maybe you want to take page screenshots of specific elements containing matching images
        var capturedImages = 0; var totalElements = 0;
        // .: Define Image Capture : Min/Max Width/Height :.
        const minWidth = 1; const minHeight = 1;
        const maxWidth = 9999;  const maxHeight = 9999;
        const regxp = new RegExp('url');
        $('*').each(function(index, el) { var ignore=false;
            // search for elements with 'background-image' css property
            if($(el).css('background-image')!=null!=null){
                var wu = $(this).css('width');
                var width = parseFloat(wu.replace('px',''));
                var hu = $(this).css('height');
                var height = parseFloat(wu.replace('px',''));
                var src = $(el).css('background-image');
                var group = "background-image"
                if(!src.match(regxp)){ignore=true;}else{
                    //remove the keep the contents inside the 'url()' string'
                    src = (($(el).css('background-image')).slice(4));
                    src = src.substring(0, src.length - 1);
                }
            }
            // search for elements with 'src' html attribute
            else if($(el).attr('src')!=null){
                var width = $(this).get(0).naturalWidth; 
                var height = $(this).get(0).naturalHeight;
                var group = "src" 
                var src = $(el).attr('src');
            }
            //---------------------------------------------------------
            if(width>=minWidth&&height>=minWidth&&
               width<=maxWidth&&height<=maxWidth&&
               !ignore){
                    IMAGES[group].indxs.push(index); 
                    IMAGES[group].ref.push(src); 
                    IMAGES.selectors.push(this); 
                    capturedImages++;
                    console.log("  [captured] :",group,width,height,src);
                    //:store clipRect for this element
                    var clipR = $.extend({},$(el).offset(),{width: $(el).offsetWidth,height: $(el).offsetHeight});
                    console.log("    (clipRect)",JSON.stringify(clipR));
                    clipRects.push(clipR);
            }
            totalElements++;
        });
        // report information :
        console.log('[i] Total Elements Parsed : ',totalElements);
        console.log('[*] Total Images Captured : ',capturedImages);
        console.log('     >              [src] : ',IMAGES['src'].indxs.length);
        console.log('     > [background-image] : ',IMAGES['background-image'].indxs.length);
      });
      console.log('[!] TO-DO : STORE CAPTURED IMAGES AS FILES');
      phantom.exit();
    });