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