模擬炒股的數據來源

來源:互聯網
上載者:User
 股票資料的擷取目前有如下兩種方法可以擷取:

1.http/javascript介面取資料
2.web-service介面

1.http/javascript介面取資料

1.1Sina股票資料介面
以大秦鐵路(股票代號:601006)為例,如果要擷取它的最新行情,只需訪問新浪的股票資料
介面:http://hq.sinajs.cn/list=sh601006這個url會返回一串文本,例如:

var hq_str_sh601006="大秦鐵路, 27.55, 27.25, 26.91, 27.55, 26.20, 26.91, 26.92,
22114263, 589824680, 4695, 26.91, 57590, 26.90, 14700, 26.89, 14300,
 26.88, 15100, 26.87, 3100, 26.92, 8900, 26.93, 14230, 26.94, 25150, 26.95, 15220, 26.96, 2008-01-11, 15:05:32";

這個字串由許多資料拼接在一起,不同含義的資料用逗號隔開了,按照程式員的思路,順序號從0開始。
0:”大秦鐵路”,股票名字;
1:”27.55″,今日開盤價;
2:”27.25″,昨日收盤價;
3:”26.91″,當前價格;
4:”27.55″,今日最高價;
5:”26.20″,今日最低價;
6:”26.91″,競買價,即“買一”報價;
7:”26.92″,競賣價,即“賣一”報價;
8:”22114263″,成交的股票數,由於股票交易以一百股為基本單位,所以在使用時,通常把該值除以一百;
9:”589824680″,成交金額,單位為“元”,為了一目瞭然,通常以“萬元”為成交金額的單位,所以通常把該值除以一萬;
10:”4695″,“買一”申請4695股,即47手;
11:”26.91″,“買一”報價;
12:”57590″,“買二”
13:”26.90″,“買二”
14:”14700″,“買三”
15:”26.89″,“買三”
16:”14300″,“買四”
17:”26.88″,“買四”
18:”15100″,“買五”
19:”26.87″,“買五”
20:”3100″,“賣一”申報3100股,即31手;
21:”26.92″,“賣一”報價
(22, 23), (24, 25), (26,27), (28, 29)分別為“賣二”至“賣四的情況”
30:”2008-01-11″,日期;
31:”15:05:32″,時間;

這個介面對於JavaScript程式非常方便,通常的使用方式為,靜態或動態地在頁面中插入:

<script type="text/javascript" src="http://hq.sinajs.cn/list=sh601006" charset="gb2312">

</script>

 <script type="text/javascript">

 var elements=hq_str_sh601006.split(",");

document.write("current price:"+elements[3]);

</script>

這段代碼輸出大秦鐵路(股票代號:601006)的當前股價

current price:14.20

如果你要同時查詢多個股票,那麼在URL最後加上一個逗號,再加上股票代號就可以了;比如你要一次查詢大秦鐵路(601006)和大同煤業(601001)的行情,就這樣使用URL:
http://hq.sinajs.cn/list=sh601003,sh601001

但如果你要查詢大盤指數,情況會有不同,比如查詢上證綜合指數(000001),使用如下URL:
http://hq.sinajs.cn/list=s_sh000001 伺服器返回的資料為:

var hq_str_s_sh000001="上證指數,3094.668,-128.073,-3.97,436653,5458126";

資料含義分別為:指數名稱,當前點數,當前價格,漲跌率,成交量(手),成交額(萬元);

查詢深圳成指的URL為:
http://hq.sinajs.cn/list=s_sz399001

對於股票的K線圖,日線圖等的擷取可以通過請求http://image.sinajs.cn/…./…/*.gif此URL擷取,其中*代表股票代號,詳見如下:

查看日K線圖:

http://image.sinajs.cn/newchart/daily/n/sh601006.gif

分時線的查詢:

http://image.sinajs.cn/newchart/min/n/sh000001.gif

日K線查詢:

http://image.sinajs.cn/newchart/daily/n/sh000001.gif

周K線查詢:

http://image.sinajs.cn/newchart/weekly/n/sh000001.gif

月K線查詢:

http://image.sinajs.cn/newchart/monthly/n/sh000001.gif

1.2              Baidu Google的財經資料
在baidu ,google中搜尋某隻股票代號時,將會在頭條顯示此股票的相關資訊,例如在google搜尋601006時,
第一條搜尋結果如:

通過點擊左邊的圖片我們發現會將此圖片連結到sina財經頻道上,也就是說google股票資料的擷取也是從sina擷取。後經抓包分析,
發現google也是採用1.1中介紹的介面。

Baidu的股票資料來自baidu的財經頻道http://stock.baidu.com.

2.   web-service介面

2.1  CHINAstock的web-service:
http://www.webxml.com.cn/WebServices/ChinaStockWebService.asmx

中國股票行情資料 WEB 服務(支援深圳和上海股市的全部基金、債券和股票),資料即時更新。輸出GIF分時走勢圖、日/周/月 K 線圖、及時行情資料(股票名稱、行情時間、最新價、昨收盤、今開盤、漲跌額、最低、最高、漲跌幅、成交量、成交額、競買價、競賣價、委比、買一 - 買五、賣一 - 賣五)。此WEB服務提供了如下幾個介面:

2.1.1  getStockImageByCode
GET 股票GIF分時走勢圖

INput:theStockCode = 股票看板代號,如:sh000001

POST /WebServices/ChinaStockWebService.asmx HTTP/1.1

Host: www.webxml.com.cn

Content-Type: text/xml; charset=utf-8

Content-Length: length

SOAPAction: "http://WebXml.com.cn/getStockImageByCode"

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

  <soap:Body>

    <getStockImageByCode xmlns="http://WebXml.com.cn/">

      <theStockCode>string</theStockCode>

    </getStockImageByCode>

  </soap:Body>

</soap:Envelope>

Output:

2.1.2 getStockImageByteByCode
獲得中國股票GIF分時走勢圖位元組數組

INput:theStockCode = 股票看板代號,如:sh000001

POST /WebServices/ChinaStockWebService.asmx HTTP/1.1Host: www.webxml.com.cnContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "http://WebXml.com.cn/getStockImageByteByCode" <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  <soap:Body>    <getStockImageByteByCode xmlns="http://WebXml.com.cn/">      <theStockCode>string</theStockCode>    </getStockImageByteByCode>  </soap:Body></soap:Envelope>

返回的資料如下:

<?xml version="1.0" encoding="utf-8" ?>

  <base64Binary xmlns="http://WebXml.com.cn/">R0lGODlhIQIsAfcAAAAAAAwLBxkGBQ4ODhAQEBsSChUVFS4TDB8eGQkA9koPCDAAzy4mFVgAp2UYC0IqEUYuBVwiDEAsI1QnFX8AgDU1NUozFlgxD6cBWVY5FnIwEmQ4Gc0AMlhDHPEADlVJMEpKSm1IHOUBWpY3FZMyVY9IGXRWIEFmWGNYUmpdPXJgHQB8HK9EGGBgX4lXIACoAHhkMyt4m4VkJtstbv8A

2.1.3  getStockImage_kByCode
直接獲得中國股票GIF日/周/月 K 線圖(545*300pixel/72dpi)
INPUT: theStockCode = 股票看板代號

      theType = K 線圖類型(D:日[預設]、W:周、M:月),

POST /WebServices/ChinaStockWebService.asmx HTTP/1.1Host: www.webxml.com.cnContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "http://WebXml.com.cn/getStockImage_kByCode" <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  <soap:Body>    <getStockImage_kByCode xmlns="http://WebXml.com.cn/">      <theStockCode>string</theStockCode>      <theType>string</theType>    </getStockImage_kByCode>  </soap:Body></soap:Envelope>

比如按照所示輸入:

返回的結果就是周K線圖:

2.1.4  getStockImage_kByteByCode
獲得中國股票GIF日/周/月 K 線圖位元組數組

Input:theStockCode = 股票看板代號,如:sh000001

POST /WebServices/ChinaStockWebService.asmx HTTP/1.1Host: www.webxml.com.cnContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "http://WebXml.com.cn/getStockImage_kByteByCode" <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  <soap:Body>    <getStockImage_kByteByCode xmlns="http://WebXml.com.cn/">      <theStockCode>string</theStockCode>      <theType>string</theType>    </getStockImage_kByteByCode>  </soap:Body></soap:Envelope>HTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8Content-Length: length <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  <soap:Body>    <getStockImage_kByteByCodeResponse xmlns="http://WebXml.com.cn/">      <getStockImage_kByteByCodeResult>base64Binary</getStockImage_kByteByCodeResult>    </getStockImage_kByteByCodeResponse>  </soap:Body></soap:Envelope>比如按照輸入

返回的結果就是周K線圖位元組數組

<?xml version="1.0" encoding="utf-8" ?>

  <base64Binary xmlns="http://WebXml.com.cn/">R0lGODlhIQIsAfcAAAAAAAwLBxkGBQ4ODhAQEBsSChUVFS4TDB8eGQkA9koPCDAAzy4mFVgAp2UYC0IqEUYuBVwiDEAsI1QnFX8AgDU1NUozFlgxD6cBWVY5FnIwEmQ4Gc0AMlhDHPEADlVJMEpKSm1IHOUBWpY3FZMyVY9IGXRWIEFmWGNYUmpdPXJgHQB8HK9EGGBgX4lXIACoAHhkMyt4m4VkJtstbv8A

2.1.5  getStockInfoByCode
獲得中國股票及時行情
input:theStockCode = 股票看板代號

POST /WebServices/ChinaStockWebService.asmx HTTP/1.1Host: www.webxml.com.cnContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "http://WebXml.com.cn/getStockInfoByCode" <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  <soap:Body>    <getStockInfoByCode xmlns="http://WebXml.com.cn/">      <theStockCode>string</theStockCode>    </getStockInfoByCode>  </soap:Body></soap:Envelope>返回的值一個一維字串數組 String(24),結構為:String(0)股票看板代號、String(1)股票名稱、String(2)行情時間、String(3)最新價(元)、String(4)昨收盤(元)、String(5)今開盤(元)、String(6)漲跌額(元)、String(7)最低(元)、String(8)最高(元)、String(9)漲跌幅(%)、String(10)成交量(手)、String(11)成交額(萬元)、String(12)競買價(元)、String(13)競賣價(元)、String(14)委比(%)、String(15)-String(19)買一 - 買五(元)/手、String(20)-String(24)賣一 - 賣五(元)/手。

Web service的方法類似於現在concurrent項目的DBWS資料的擷取,都是通過SOAP協議向DBWS伺服器擷取相關的資料。

利用雅虎查中國股票

http://quote.yahoo.com/d/quotes.csv?s=MSFT&f=slc1wop
返回微軟的股票價格

"MSFT","4:00pm - <b>30.70</b>",+1.04,"21.46 - 30.75",29.77,29.66
http://quote.yahoo.com/d/quotes.csv?s=000969.SZ&f=slc1wop
這個返回安泰科技的,一般有半個小時的延遲。

但是s=000969.sz 這個後面的sz是什麼意思呢?
深圳:)
滬市是SS尾碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.