【问题标题】:How to download stock price data only when it is not erroneous (404)?如何仅在没有错误(404)的情况下下载股票价格数据?
【发布时间】:2014-12-09 23:57:33
【问题描述】:

脚本从 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>

【问题讨论】:

    标签: javascript http-status-code-404 casperjs stockquotes


    【解决方案1】:

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

    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();
    

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


    我真的试过了,你的页面很奇怪,下载不成功。您可以加载一些虚拟页面,例如 example.com,使用__utils__.sendAJAX(只能从页面上下文访问)自己下载 csv 文件,并使用 fs 模块write 下载它们。您应该只根据您确定的特定 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();
    

    【讨论】:

      猜你喜欢
      • 2018-11-20
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      相关资源
      最近更新 更多