当我使用普通浏览器(Chrome)浏览网站A时,当我点击网站A上的链接时,Chrome会以CSV文件的形式下载报告.
当我检查服务器响应头时,我得到以下结果:
Cache-Control:private,max-age=31536000
Connection:Keep-Alive
Content-Disposition:attachment; filename="report.csv"
Content-Encoding:gzip
Content-Language:de-DE
Content-Type:text/csv; charset=UTF-8
Date:Wed, 22 Jul 2015 12:44:30 GMT
Expires:Thu, 21 Jul 2016 12:44:30 GMT
Keep-Alive:timeout=15, max=75
Pragma:cache
Server:Apache
Transfer-Encoding:chunked
Vary:Accept-Encoding
Run Code Online (Sandbox Code Playgroud)
现在,我想使用PhantomJS下载并解析此文件.我设置了page onResourceReceived监听器,看看Phantom是否会接收/下载该文件.
clientRequests.phantomPage.onResourceReceived = function(response) {
console.log('Response (#' + response.id + ', stage "' + response.stage + '"): ' + JSON.stringify(response));
};
Run Code Online (Sandbox Code Playgroud)
当我发出Phantom请求下载文件时(这是page.open('文件的URL')),我可以在Phantom日志中看到该文件已下载.这是日志:
"contentType": "text/csv; charset=UTF-8",
"headers": {
"name": "Date",
"value": "Wed, 22 Jul 2015 12:57:41 GMT"
},
"name": "Content-Disposition",
"value": "attachment; filename=\"report.csv\"",
"status":200,"statusText":"OK"
Run Code Online (Sandbox Code Playgroud)
我收到了文件及其内容,但是如何访问文件数据?当我打印当前的PhantomJS page对象时,我得到了页面A的HTML而我不希望这样,我想要CSV文件,我需要使用JavaScript解析它.
我想下载一个CSV文件,它是通过POST请求点击按钮生成的.我在casperJs和phantomJS论坛上进行了最好的研究并空手而归.在像firefox这样的普通浏览器中,在发布请求后会出现浏览器下载对话窗口.如何在PhantomJS中处理这种情况
TTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
Content-disposition: attachment;filename=ExportData.csv
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Fri, 19 Apr 2013 23:26:40 GMT
Content-Length: 65183
Run Code Online (Sandbox Code Playgroud)