0

该脚本从 Finance.yahoo.com 下载历史股票价格。代码数组用于循环脚本,根据代码数组创建链接并下载与每个代码相关的数据。但是,一些股票代码不再是最新的,因此雅虎提供了 404 页面而不是包含价格信息的 csv。然后将错误页面存储在 csv 中并保存到我的计算机中。为了不下载这些文件,我正在寻找字符串“对不起,找不到您请求的页面。”,它包含在每个雅虎错误站点中,作为 404 页面的指示符。

代码的行为(输出,见下面的代码):

该代码贯穿所有股票代码并下载所有股票价格 .csv。这适用于所有股票代码,但雅虎不再使用某些股票代码。对于不再使用的股票代码,程序会下载一个包含 yahoos 404 页面的 .csv。所有文件(以及包含实际数据的好文件)都下载到目录 c:\Users\W7ADM\stock-price-leecher\data2 中。

问题:

我希望代码不要将 404 页面下载到 csv 文件中,但在这种情况下什么都不做,然后继续循环中的下一个股票代码。我正在尝试使用查找字符串“抱歉,找不到您请求的页面”的 if 条件来实现这一点。显示在 yahoos 404 页面上。最后,我希望下载实际存在的所有 csv 代码并将它们保存到我的硬盘中。

var url_begin = 'http://real-chart.finance.yahoo.com/table.csv?s=';
var url_end = '&a=00&b=1&c=1950&d=11&e=31&f=2050&g=d&ignore=.csv';
var tickers = [];
var link_created = '';

var casper = require('casper').create({
    pageSettings: {
        webSecurityEnabled: false
    }
});                   

casper.start('http://www.google.de', function() {              
        tickers = ['ADS.DE', '0AM.DE']; //ADS.DE is retrievable, 0AM.DE is not
        //loop through all ticker symbols
        for (var i in tickers){
                //create a link with the current ticker
                link_created=url_begin + tickers[i] + url_end;
                //check to see, if the created link returns a 404 page
                this.open(link_created);
                var content = this.getHTML();
                //If is is a 404 page, jump to the next iteration of the for loop
                if (content.indexOf('Sorry, the page you requested was not found.')>-1){
                        console.log('No Page found.');
                        continue; //At this point I want to jump to the next iteration of the loop.
                }
                //Otherwise download file to local hdd
                else {
                        console.log(link_created);
                        this.download(link_created, 'stock-price-leecher\\data2\\'+tickers[i]+'.csv');
                }
        }
});

casper.run(function() {
        this.echo('Ende...').exit();
});

输出:

C:\Users\Win7ADM>casperjs spl_old.js
ADS.DE,0AM.DE
http://real-chart.finance.yahoo.com/table.csv?s=ADS.DE&a=00&b=1&c=1950&d=11&e=31
&f=2050&g=d&ignore=.csv
http://real-chart.finance.yahoo.com/table.csv?s=0AM.DE&a=00&b=1&c=1950&d=11&e=31
&f=2050&g=d&ignore=.csv
Ende...

C:\Users\Win7ADM>
4

1 回答 1

0

casper.open是异步的(非阻塞的),但是你以阻塞的方式使用它。您应该使用casper.thenOpenwhich 有一个回调,该回调在页面加载时被调用,您可以用它做一些事情。

casper.start("http://example.com");

tickers = ['ADS.DE', '0AM.DE']; //ADS.DE is still retrievable, 0AM.DE is not
tickers.forEach(function(ticker){
    var link_created = url_begin + ticker + url_end;
    casper.thenOpen(link_created, function(){
        console.log("open", link_created);
        var content = this.getHTML();
        if (content.indexOf('Sorry, the page you requested was not found.') > -1) {
            console.log('No Page found.');
        } else {
            console.log("downloading...");
            this.download(link_created, 'test14_'+ticker+'.csv');
        }
    });
});

casper.run();

除了使用thenOpen回调之外,您还可以注册到page.resource.received事件并通过检查状态来专门下载它。但是现在您将无法访问,ticker因此您必须将其存储在全局变量中或从resource.url.

var i = 0;
casper.on("page.resource.received", function(resource){
    if (resource.stage === "end" && resource.status === 200) {
        this.download(resource.url, 'test14_'+(i++)+'.csv');
    }
});

casper.start("http://example.com");

tickers = ['ADS.DE', '0AM.DE']; //ADS.DE is still retrievable, 0AM.DE is not
tickers.forEach(function(ticker){
    var link_created = url_begin + ticker + url_end;
    casper.thenOpen(link_created);
});

casper.run();

我认为您不应该使用openor来执行此操作thenOpen。它可能适用于 PhantomJS,但可能不适用于 SlimerJS。


我实际上尝试过,您的页面很奇怪,因为下载没有成功。您可以加载一些虚拟页面,例如 example.com,使用自己下载 csv 文件__utils__.sendAJAX(只能从页面上下文访问)并使用 fs 模块编写它们。您应该只根据您确定的特定 404 错误页面文本编写它:

casper.start("http://example.com");

casper.then(function(){
    tickers = ['ADS.DE', '0AM.DE']; //ADS.DE is still retrievable, 0AM.DE is not
    tickers.forEach(function(ticker){
        var link_created = url_begin + ticker + url_end;
        var content = casper.evaluate(function(url){
            return __utils__.sendAJAX(url, "GET");
        }, link_created);
        console.log("len: ", content.length);
        if (content.indexOf('Sorry, the page you requested was not found.') > -1) {
            console.log('No Page found.');
        } else {
            console.log("writing...");
            fs.write('test14_'+ticker+'.csv', content);
        }
    });
});

casper.run();
于 2014-12-09T15:55:38.493 回答