URL 被重定向时如何使用 CasperJS 下载
How to download with CasperJS when the URL is being redirected
我花了几天时间阅读答案和测试,并试图弄清楚如何在重定向 URL 时访问 CasperJS 下载文件。我在尝试从 https://firefox.com 下载 Firefox 时重现了我的问题 我收到警告:
[warning] [phantom] Loading resource failed with status=fail (HTTP 200): https://download.mozilla.org/?product=firefox-48.0.2-SSL&os=linux64&lang=en-US
[warning] [phantom] Loading resource failed with status=fail (HTTP 200): https://download-installer.cdn.mozilla.net/pub/firefox/releases/48.0.2/linux-x86_64/en-US/firefox-48.0.2.tar.bz2
和一个名为 ?product=firefox-48.0.2-SSL&os=linux64&lang=en-US
的 0 字节文件
第二个警告告诉我 casperjs 获得了新的 url(如果您使用浏览器导航到它们,则两者下载相同的 zip 文件)
我缺少什么来捕获下载的文件?
var casper = require('casper').create({
pageSettings: {
userAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
}
});
casper.start().thenOpen("https://firefox.com", function () {
this.viewport(1200, 800);
});
casper.then(function () {
this.click('li.os_linux64 a');
this.wait(3000);
});
casper.on('resource.received', function (resource) {
if (resource.stage !== "end") {
return;
}
if (resource.url.indexOf('download') > -1) {
this.download(resource.url, 'out/' + new String(resource.url).substring(resource.url.lastIndexOf('/') + 1));
}
});
casper.run();
版本:
casperjs 1.1.3
phantomjs 2.1.1
命令行:
casperjs --verbose --log-level=warning --ssl-protocol=any --ignore-ssl-errors=true --web-security=no script.js
我回答了我自己的问题。我看到的所有例子都有
if (resource.stage !== "end") {
return;
}
在 casper.on('resource.received'...
函数中。 删除 这导致下载成功。我不确定它做什么(或现在不做什么)。
注意:我还必须使用较小的下载文件进行测试,因为 casperjs/phantomjs 资源接收似乎有 30 秒的超时。见 CasperJS File Download Times Out After 30 Seconds
我花了几天时间阅读答案和测试,并试图弄清楚如何在重定向 URL 时访问 CasperJS 下载文件。我在尝试从 https://firefox.com 下载 Firefox 时重现了我的问题 我收到警告:
[warning] [phantom] Loading resource failed with status=fail (HTTP 200): https://download.mozilla.org/?product=firefox-48.0.2-SSL&os=linux64&lang=en-US
[warning] [phantom] Loading resource failed with status=fail (HTTP 200): https://download-installer.cdn.mozilla.net/pub/firefox/releases/48.0.2/linux-x86_64/en-US/firefox-48.0.2.tar.bz2
和一个名为 ?product=firefox-48.0.2-SSL&os=linux64&lang=en-US
第二个警告告诉我 casperjs 获得了新的 url(如果您使用浏览器导航到它们,则两者下载相同的 zip 文件)
我缺少什么来捕获下载的文件?
var casper = require('casper').create({
pageSettings: {
userAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
}
});
casper.start().thenOpen("https://firefox.com", function () {
this.viewport(1200, 800);
});
casper.then(function () {
this.click('li.os_linux64 a');
this.wait(3000);
});
casper.on('resource.received', function (resource) {
if (resource.stage !== "end") {
return;
}
if (resource.url.indexOf('download') > -1) {
this.download(resource.url, 'out/' + new String(resource.url).substring(resource.url.lastIndexOf('/') + 1));
}
});
casper.run();
版本:
casperjs 1.1.3
phantomjs 2.1.1
命令行:
casperjs --verbose --log-level=warning --ssl-protocol=any --ignore-ssl-errors=true --web-security=no script.js
我回答了我自己的问题。我看到的所有例子都有
if (resource.stage !== "end") {
return;
}
在 casper.on('resource.received'...
函数中。 删除 这导致下载成功。我不确定它做什么(或现在不做什么)。
注意:我还必须使用较小的下载文件进行测试,因为 casperjs/phantomjs 资源接收似乎有 30 秒的超时。见 CasperJS File Download Times Out After 30 Seconds