使用PhantomJs下载文件

问题描述 投票:3回答:1

我正在尝试使用PhantomJs无头浏览器下载文件。我必须按下以开始下载的按钮是Javascript格式。我不知道如何处理下载对话框,我找不到有关它的文档。

我需要登录才能执行下载。

有没有人关于这种行为的信息?

python python-2.7 phantomjs headless
1个回答
1
投票

尝试将此代码放在按钮单击事件中,或者可以使用此原始代码来获取它:

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

page.settings.userName = 'your username here';

page.settings.password = 'your password here';

 var url = "your url comes here";
 var fs = require('fs');
 var path = 'index.html'; //here you can use a format of the file you want .json .txt etc.

 page.open(url, function (status) {
     if(status !== 'success') {
         console.log('Connection failed, page was not loaded!');
     } else {
         var content = page.content;
         fs.write(path,content,'w')
         phantom.exit();
     }
});

另外,看看这个链接How to download a csv file using PhantomJS

如果您需要进一步的帮助,请告诉我!

© www.soinside.com 2019 - 2024. All rights reserved.