如何获取CasperJS请求的图像的二进制文件? [英] How to get the binary of the image requested by CasperJS?

查看:94
本文介绍了如何获取CasperJS请求的图像的二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

casper.on("resource.received", function (response) {
    if (response.url.indexOf('checkcode') != -1) {
        this.log('response: ' + JSON.stringify(response), 'debug');
    }
})

输出:

[debug] [phantom] [2016-04-21T01:36:46.0​​07Z]回复: {"body":","bodySize":1847,"contentType":"image/png","headers":[{"name":"Server","value":"Tengine/2.1.0"} ,{"name":"Date","value":"Thu, 2016年4月21日01:36:45 GMT},{" name:" Content-Type," value:" image/png},{" name:" Content-Length," value:" 1847},{" name":"Connection","value":保持活动"},{"name":严格传输安全","value":"max-age = 31536000"},{"name":"Cache- Control","value":"no-cache, 私人的, 必须重新验证},{" name:" Pragma," value:" no-cache},{" name:" Expires," value:" Fri, 1990年1月1日00:00:00 GMT},{" name:" Set-Cookie," value:" JSESSIONID = 48565DE1A6030B587CB9D14F9B53FBB4; 路径=/; HttpOnly \ nJSESSIONID = 48565DE1A6030B587CB9D14F9B53FBB4;路径=; 安全的; HttpOnly \ nspanner = Kkyr7NMMCJ + YBPeL1x6AIm/qefJ/jCic4EJoL7C0n0A =; path =/; secure;}]," id:16," redirectURL:null," stage:" start," status:200," statusText:" OK," time:" 2016-04-21T01:36:46.0​​07Z," url:" 解决方案

ArtjomB评论:

PhantomJS中没有响应内容

所以我用this.captureBase64('png', '#J-checkcode-img')对其进行了入侵.

Code:

casper.on("resource.received", function (response) {
    if (response.url.indexOf('checkcode') != -1) {
        this.log('response: ' + JSON.stringify(response), 'debug');
    }
})

Output:

[debug] [phantom] [2016-04-21T01:36:46.007Z] response: {"body":"","bodySize":1847,"contentType":"image/png","headers":[{"name":"Server","value":"Tengine/2.1.0"},{"name":"Date","value":"Thu, 21 Apr 2016 01:36:45 GMT"},{"name":"Content-Type","value":"image/png"},{"name":"Content-Length","value":"1847"},{"name":"Connection","value":"keep-alive"},{"name":"Strict-Transport-Security","value":"max-age=31536000"},{"name":"Cache-Control","value":"no-cache, private, must-revalidate"},{"name":"Pragma","value":"no-cache"},{"name":"Expires","value":"Fri, 01 Jan 1990 00:00:00 GMT"},{"name":"Set-Cookie","value":"JSESSIONID=48565DE1A6030B587CB9D14F9B53FBB4; Path=/; HttpOnly\nJSESSIONID=48565DE1A6030B587CB9D14F9B53FBB4; Path=; Secure; HttpOnly\nspanner=Kkyr7NMMCJ+YBPeL1x6AIm/qefJ/jCic4EJoL7C0n0A=;path=/;secure;"}],"id":16,"redirectURL":null,"stage":"start","status":200,"statusText":"OK","time":"2016-04-21T01:36:46.007Z","url":"https://omeo.alipay.com/service/checkcode?sessionID=94e0a91bab9e202d3ee2e574e1c8f245&t=0.6211719120304562"}

[debug] [phantom] [2016-04-21T01:36:46.011Z] response: {"contentType":"image/png","headers":[{"name":"Server","value":"Tengine/2.1.0"},{"name":"Date","value":"Thu, 21 Apr 2016 01:36:45 GMT"},{"name":"Content-Type","value":"image/png"},{"name":"Content-Length","value":"1847"},{"name":"Connection","value":"keep-alive"},{"name":"Strict-Transport-Security","value":"max-age=31536000"},{"name":"Cache-Control","value":"no-cache, private, must-revalidate"},{"name":"Pragma","value":"no-cache"},{"name":"Expires","value":"Fri, 01 Jan 1990 00:00:00 GMT"},{"name":"Set-Cookie","value":"JSESSIONID=48565DE1A6030B587CB9D14F9B53FBB4; Path=/; HttpOnly\nJSESSIONID=48565DE1A6030B587CB9D14F9B53FBB4; Path=; Secure; HttpOnly\nspanner=Kkyr7NMMCJ+YBPeL1x6AIm/qefJ/jCic4EJoL7C0n0A=;path=/;secure;"}],"id":16,"redirectURL":null,"stage":"end","status":200,"statusText":"OK","time":"2016-04-21T01:36:46.010Z","url":"https://omeo.alipay.com/service/checkcode?sessionID=94e0a91bab9e202d3ee2e574e1c8f245&t=0.6211719120304562"}

And I got those logs, but there is no binary in the response, so I can't get the image...

So, how to get the binary of the image requested by CasperJS?

解决方案

As ArtjomB commented:

The response content is not available in PhantomJS

So I hack it by this.captureBase64('png', '#J-checkcode-img').

这篇关于如何获取CasperJS请求的图像的二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆