cover_image

枪在手,跟我走,直通股神之路

强大的 新浪云计算 2016年06月29日 03:58

每个股民心中,都有一个股神梦,然而手眼通天的股神并不是凭空掐指一算就可以知道股市的涨涨跌跌的。需要详细剖析股票行情,掌握股票走势,必须有足够多足够及时准确的股票实时数据做支撑,才能洞若观火。

图片

如今新浪云开放新浪财经权威高质量的股票实时报价数据,独家内网访问财经数据加上微博同款的CDN服务,真正的做到几乎无延迟,价格超级便宜,只要1元钱就可以任性调用一百万次。开发者们可以方便快捷的开发各种炒股神器。

调用方法简单便捷。

首先,进入应用,选择“大数据”分类下的“股票实时信息查询”,在页面中可以方便的看到昨天以及历史请求的总次数。


图片


那么,怎么调用API获取数据呢?还是要亮出代码的!


图片

当然,可以不生产代码,只做代码的搬运工,所以,这里给出两个例子,分别是在新浪云的PHP环境中,在网页里调用API以及在本地使用Python调用API获取股票数据

 

所以,不愿意自己写代码的亲们可以复制粘贴一下代码就直接可以使用啦,是不是很周到呢~

 

先来看PHP的:


<?php

class CloudApi

{

   private $accessKey;

   private $secretKey;

   private $gapi = 'http://g.sae.sina.com.cn';

   public function __construct($accessKey, $secretKey)

    {

       $this->accessKey = $accessKey;

       $this->secretKey = $secretKey;

    }

   public function get($uri)

    {

       if (!$uri) {

           return false;

       }

       return $this->_curl($uri);

    }

   private function _cal_sign_and_set_header($ch, $uri)

    {

       $method = 'GET';

       $a = array();

       $a[] = $method;

       $a[] = $uri;

       // $timeline unix timestamp

       $timeline = time();

       $b = array('x-sae-accesskey' => $this->accessKey,'x-sae-timestamp' => $timeline);

       ksort($b);

       foreach ($b as $key => $value) {

           $a[] = sprintf("%s:%s", $key, $value);

       }

       $str = implode("\n", $a);

       $s = hash_hmac('sha256', $str,$this->secretKey, true);

       $b64_s = base64_encode($s);

       $headers = array();

       $headers[] = sprintf('x-sae-accesskey:%s', $this->accessKey);

       $headers[] = sprintf('x-sae-timestamp:%s', $timeline);

        $headers[] = sprintf('Authorization:SAEV1_HMAC_SHA256 %s', $b64_s);

       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

       return $headers;

    }

   private function _curl($uri)

    {

       $ch = curl_init();

       $url = sprintf('%s%s', $this->gapi, $uri);

       curl_setopt($ch, CURLOPT_URL, $url);

       $headers = $this->_cal_sign_and_set_header($ch, $uri);

       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

       $txt = curl_exec($ch);

       $error = curl_errno($ch);

        curl_close($ch);

       if ($error) {

           return false;

       }

       return $txt;

    }

}

 

header("Content-type:text/plain;charset=gbk");

$i = new CloudApi(SAE_ACCESSKEY,SAE_SECRETKEY);

$ret =$i->get('/financehq/list=sh000001');

var_dump($ret)


什么都不用动,直接将上面的代码上传到应用下,然后通过浏览器访问,就可以在页面里看到:


图片


数据拿到了!是不是超简单~

 

再看看Python的:


相对要简单一点

#!/usr/bin/env python

import hmac

import base64

import hashlib

import time

import urllib2

accesskey = '替换成应用Accesskey

secretkey = '替换成应用SecretKey'

timestamp = str(int(time.time()))

host = 'http://g.sae.sina.com.cn'

uri = '/financehq/list=sh000001'

url = host + uri

 

msg = "GET\n" + uri +"\nx-sae-accesskey:" + accesskey + "\nx-sae-timestamp:" +timestamp

 

h = hmac.new(secretkey, msg,hashlib.sha256).digest()

s = base64.b64encode(h)

 

req = urllib2.Request(url)

req.add_header('x-sae-accesskey',accesskey)

req.add_header('x-sae-timestamp',timestamp)

req.add_header('Authorization',"SAEV1_HMAC_SHA256 " + s)

printurllib2.urlopen(req).read().decode('gbk')

 

把上面的代码保存为api.py,将其中的accesskeysecretkey替换成自己应用的。然后直接运行python api.py就可以输出结果啦~


图片

就是这么简单~轻松加愉快的获取新浪财经的权威高质量股票实时数据


更多财经大数据,如基本面数据,股票历史数据、N日均线数据等即将震撼上线!敬请期待哦~~


欢迎小伙伴评论文章,写下你最希望用的数据,说不定过几天就有了呢~



图片
·

继续滑动看下一个
新浪云计算
向上滑动看下一个