PhantomJS:使用Cloudflare Protection从网站下载文件

时间:2019-03-09 18:44:52

标签: bash debian phantomjs casperjs

我有此代码,想要下载文件。解析受保护站点的源代码可以正常工作,但Filedownload不起作用。也许有人可以告诉我错误在哪里?

"use strict";
var page = require('webpage').create(),
    system = require('system'),
    address, delay;

 var fs = require('fs');

if (system.args.length < 3 || system.args.length > 5) {
    console.log('Usage: delay.js URL delay');
    phantom.exit(1);
} else {
    address = system.args[1];
    delay = system.args[2];
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('Unable to load the address!');
            phantom.exit(1);
        } else {
            window.setTimeout(function () {
                var content = page.content;
                fs.write(address);
                console.log(content);
                phantom.exit();
            }, delay);
        }
    });
}

我是这样执行的:

phantomjs delay.js https://my.url/file.txt 10000

1 个答案:

答案 0 :(得分:1)

好,我解决了。以前使用Python并安装了“ cfscrape”。

import cfscrape
import sys

scraper = cfscrape.create_scraper()

url = (sys.argv[1])
cfurl = scraper.get(url).content
name = url.split('/')[-1]

with open(name, 'wb') as f:
    f.write(cfurl)