scala获取免费的股票日k线数据

接口的的抓取使用了Scala标准库的Source


class KLineModel {
var dateStr ="";
var openPrice = 0f;
var closePrice = 0f;
var highPrice = 0f;
var lowPrice = 0f;

private var stockInfo :String =""

def this(stockInfo:String)
{
this()
this.stockInfo=stockInfo /** 根据腾讯的数据接口解析数据 **/
val stockDetail=stockInfo.split(Array(' ',' ',' ',' ',' '))
if (stockDetail.length>4){
this.dateStr=stockDetail(0)
this.openPrice=stockDetail(1).toFloat
this.closePrice =stockDetail(2).toFloat
this.highPrice=stockDetail(3).toFloat
this.lowPrice =stockDetail(4).toFloat

}
}


override def toString = s"KLineModel($dateStr, $openPrice, $closePrice, $highPrice, $lowPrice)"
import scala.io.Source
object KLineAnalyse {
def main(args: Array[String]): Unit = {
println("查询日k线股票 http://data.gtimg.cn/flashdata/hushen/daily/19/sh603000.js")
val sinaStockStream = Source.fromURL("http://data.gtimg.cn/flashdata/hushen/daily/19/sh603000.js","utf-8")
val sinaLines=sinaStockStream.getLines
for(line <- sinaLines) { /** 将每行数据解析成SinaStock对象,并答应对应的股票信息 **/
if(line.length > 20) {
println(new KLineModel(line).toString)
}
}
sinaStockStream.close()
}

}

关注公众号“大模型全栈程序员”回复“小程序”获取1000个小程序打包源码。更多免费资源在http://www.gitweixin.com/?p=2627

发表评论

邮箱地址不会被公开。 必填项已用*标注