gpt4 book ai didi

phantomjs - 在 SlimerJS 中处理下载对话框

转载 作者:行者123 更新时间:2023-12-02 04:43:24 28 4
gpt4 key购买 nike

我写了一个脚本,点击一个可以下载 mp3 文件的链接。我面临的问题是当脚本模拟点击该链接时,会弹出一个下载对话框,如下所示:
Download Dialog Box

现在,我想将此文件保存到我选择的某个路径并自动执行整个过程。我对如何处理这个对话框一无所知。

最佳答案

这是改编自 this blog post 的脚本下载文件。

In SlimerJS it is possible to use response.body inside the onResourceReceived handler. However to prevent using too much memory it does not get anything by default. You have to first set page.captureContent to say what you want. You assign an array of regexes to page.captureContent to say which files to receive. The regex is applied to the mime-type. In the example code below I use /.*/ to mean "get everything". Using [/^image/.+$/] should just get images, etc.

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

fs.makeTree('contents');

page.captureContent = [ /.*/ ];

page.onResourceReceived = function(response) {

if(response.stage!="end" || !response.bodySize)
{
return;
}

var matches = response.url.match(/[/]([^/]+)$/);
var fname = "contents/"+matches[1];

console.log("Saving "+response.bodySize+" bytes to "+fname);
fs.write(fname,response.body);

phantom.exit();
};

page.onResourceRequested = function(requestData, networkRequest) {
//console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData));
};

page.open("http://....mp3", function(){

});

关于phantomjs - 在 SlimerJS 中处理下载对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35433334/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com