• 实时股票数据接口大全


    股票数据的获取目前有如下两种方法可以获取:
    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.
    1.3              其他方式
    除了sina,baidu等网站提供股票信息外,其他网站也有类似的接口。我们分析了一款论坛上采用的股票插件,
    其中有关于实时股票数据获取的介绍,详见如下代码,其中可以看到有些数据来自sina。
    <%
    ‘==========================
    ‘ file: stock_getdata.asp
    ‘ version: 1.0.0
    ‘ copyright (c) czie.com all rights reserved.
    ‘ web: 
    http://www.czie.com
    ‘==========================
    function gethttp(rurl)
                       dim xml
                       on error resume next
                       set xml=server.createobject("Microsoft.XMLHTTP")
                       xml.open "get",rurl,false
                       xml.send
                       if not xml.readystate=4 or not xml.status=200 or err then gethttp="":exit function
                       gethttp=xml.responsetext
                       set xml=nothing
             end function
             function getstockdata(code)
                       ‘0=股票名称,1=开盘价格,2=昨收盘价格,3=当前价格,4=最高价,5=最低价,30,31=更新时间
                       dim checkcode,stockdata,stockdatasplit
                       if len(code)<5 then stockdata="0,0,0,0,0,0,0,0,0,0,0,0":exit function
                       checkcode=mid(code,len(code)-5,1)
                       if int(checkcode)<=4 then
                                stockdata=gethttp("
    http://hq.sinajs.cn/list=sz"&code&"")
                                if not len(stockdata)=0 then stockdata=split(stockdata,chr(34))(1)
                       end if
                       if int(checkcode)>=5 then
                                stockdata=gethttp("
    http://hq.sinajs.cn/list=sh"&code&"")
                                if not len(stockdata)=0 then stockdata=split(stockdata,chr(34))(1)
                       end if
                       if len(stockdata)=0 then
                                stockdata="0,0,0,0,0,0,0,0,0,0,0,0"
                       else
                                stockdatasplit=split(stockdata,",")
                       stockdata=""&exstock.checkstr(stockdatasplit(0))&","&stockdatasplit(1)&","&stockdatasplit(2)&","&stockdatasplit(3)&","&stockdatasplit(4)&","&stockdatasplit(5)&","&formatdatetime(""&stockdatasplit(30)&" "&stockdatasplit(31)&"",0)&""
                       end if
                       ‘0=股票名称,1=开盘价格,2=昨收盘价格,3=当前价格,4=最高价,5=最低价,6=更新时间
                       getstockdata=stockdata
             end function
             function getstockimg(code)
                       dim rndnum,addnum,checkcode,imgsource
                       if len(code)<5 then exit function
                       addnum=4
                       randomize:rndnum=cint(rnd*addnum)
                       select case rndnum
                                case 0
                                         getstockimg="
    http://www.10jqka.com.cn/curve/kline/?code="&code&""
                                         imgsource="
    http://www.10jqka.com.cn"
                                case 1
                                         getstockimg="
    http://stock.jrj.com.cn/htmdata/KLINE/"&code&".png"
                                         imgsource="
    http://stock.jrj.com.cn"
                                case 2
                                         checkcode=mid(code,len(code)-5,1)
                                         if int(checkcode)<=4 then
                                                   getstockimg="
    http://image.sinajs.cn/newchart/daily/n/sz"&code&".gif"
                                         end if
                                         if int(checkcode)>=5 then
                                                   getstockimg="
    http://image.sinajs.cn/newchart/daily/n/sh"&code&".gif"
                                         end if
                                         imgsource="
    http://finance.sina.com.cn"
                                case 3
                                         getstockimg="
    http://hq.gazxfe.com/stockchart/realline.chart?"&code&"&1003&SZ 500 330"
                                         imgsource="
    http://hq.gazxfe.com"
                                case 4
                                         getstockimg="
    http://chartse.stockstar.com/chartserver?code="&code&""
                                         imgsource="
    http://www.stockstar.com/"
                       end select
                       getstockimg=split(""&getstockimg&"||"&imgsource&"","||")
             end function
             function getastockimg()
                       dim rndnum,addnum,checkcode
                       dim getastockimgb,imgsource
                       addnum=6
                       randomize:rndnum=cint(rnd*addnum)
                       select case rndnum
                                case 0
                                         getastockimg="
    http://202.109.106.1/gifchartse/gif/000001.gif"
                                         getastockimgb="
    http://202.109.106.1/gifchartse/gif/399001.gif"
                                         imgsource="
    http://www.stockstar.com/"
                                case 1
                                         getastockimg="
    http://money.163.com/special/100.gif?C39"
                                         getastockimgb="
    http://money.163.com/special/101.gif?HrS"
                                         imgsource="
    http://www.163.com"
                                case 2
                                         getastockimg="
    http://www.10jqka.com.cn/curve/realtime/index2.php?code=1a0001&w=180&h=140"
                                         getastockimgb="
    http://www.10jqka.com.cn/curve/realtime/index2.php?code=399001&w=180&h=140"
                                         imgsource="
    http://www.10jqka.com.cn"
                                case 3
                                         getastockimg="
    http://chart.cnlist.com/stockchart/realline.chart?1a0001&1002&SZ 180 140"
                                         getastockimgb="
    http://chart.cnlist.com/stockchart/realline.chart?399001&1002&SZ 180 140"
                                         imgsource="
    http://chart.cnlist.com/"
                                case 4
                                         getastockimg="
    http://image.sinajs.cn/newchart/small/ish000001.gif?1189176558328"
                                         getastockimgb="
    http://image.sinajs.cn/newchart/small/isz399001.gif?1189176558328"
                                         imgsource="
    http://www.sinajs.cn"
                                case 5
                                         getastockimg="
    http://218.1.72.66/cgi/pic/sh/realtime/JA000001164143.png"
                                         getastockimgb="
    http://218.1.72.66/cgi/pic/sz/realtime/JA399001164143.png"
                                         imgsource="
    http://www.cnstock.com/"
                                case 6
                                         getastockimg="
    http://222.73.29.85/img/000001.png"
                                         getastockimgb="
    http://222.73.29.85/img/399001.png"
                                         imgsource="
    http://www.eastmoney.com/"
                       end select
                       getastockimg=split(""&getastockimg&"||"&getastockimgb&"||"&imgsource&"","||")
             end function
    %>
    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="
    R0lGODlhIQIsAfcAAAAAAAwLBxkGBQ4ODhAQEBsSChUVFS4TDB8eGQkA9koPCDAAzy4mFVgAp2UYC0IqEUYuBVwiDEAsI1QnFX8AgDU1NUozFlgxD6cBWVY5FnIwEmQ4Gc0AMlhDHPEADlVJMEpKSm1IHOUBWpY3FZMyVY9IGXRWIEFmWGNYUmpdPXJgHQB8HK9EGGBgX4lXIACoAHhkMyt4m4VkJtstbv8A">[url]http://WebXml.com.cn/">R0lGODlhIQIsAfcAAAAAAAwLBxkGBQ4ODhAQEBsSChUVFS4TDB8eGQkA9koPCDAAzy4mFVgAp2UYC0IqEUYuBVwiDEAsI1QnFX8AgDU1NUozFlgxD6cBWVY5FnIwEmQ4Gc0AMlhDHPEADlVJMEpKSm1IHOUBWpY3FZMyVY9IGXRWIEFmWGNYUmpdPXJgHQB8HK9EGGBgX4lXIACoAHhkMyt4m4VkJtstbv8A[/url]
    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="
    R0lGODlhIQIsAfcAAAAAAAwLBxkGBQ4ODhAQEBsSChUVFS4TDB8eGQkA9koPCDAAzy4mFVgAp2UYC0IqEUYuBVwiDEAsI1QnFX8AgDU1NUozFlgxD6cBWVY5FnIwEmQ4Gc0AMlhDHPEADlVJMEpKSm1IHOUBWpY3FZMyVY9IGXRWIEFmWGNYUmpdPXJgHQB8HK9EGGBgX4lXIACoAHhkMyt4m4VkJtstbv8A">[url]http://WebXml.com.cn/">R0lGODlhIQIsAfcAAAAAAAwLBxkGBQ4ODhAQEBsSChUVFS4TDB8eGQkA9koPCDAAzy4mFVgAp2UYC0IqEUYuBVwiDEAsI1QnFX8AgDU1NUozFlgxD6cBWVY5FnIwEmQ4Gc0AMlhDHPEADlVJMEpKSm1IHOUBWpY3FZMyVY9IGXRWIEFmWGNYUmpdPXJgHQB8HK9EGGBgX4lXIACoAHhkMyt4m4VkJtstbv8A[/url]
    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后缀

     =========

    1.获取实时数据

    http://finance.yahoo.com/d/quotes.csv?s=股票名称&f=数据列选项

    s — 表示股票名称,多个股票之间使用英文加号分隔,如“XOM+BBDb.TO+JNJ+MSFT”,罗列了四个公司的股票:XOM, BBDb.TO, JNJ, MSFT。

    f — 表示返回数据列,如“snd1l1yr”。更详细的参见附录

    2.历史数据请求

    http://ichart.yahoo.com/table.csv?s=string&a=int&b=int&c=int&d=int&e=int&f=int&g=d&ignore=.csv

    s — 股票名称

    a — 起始时间,月

    b — 起始时间,日

    c — 起始时间,年

    d — 结束时间,月

    e — 结束时间,日

    f — 结束时间,年

    g — 时间周期。

    • 参数g的取值范围:d->‘日’(day), w->‘周’(week),m->‘月’(mouth),v->‘dividends only’
    • 月份是从0开始的,如9月数据,则写为08。

    示例

    查询浦发银行2010.09.25 – 2010.10.8之间日线数据

    http://ichart.yahoo.com/table.csv?s=600000.SS&a=08&b=25&c=2010&d=09&e=8&f=2010&g=d

    查看国内沪深股市的股票,规则是:沪股代码末尾加.ss,深股代码末尾加.sz。如浦发银行的代号是:600000.SS

    附录

    雅虎股票API f参数对照表

    a

    Ask

    a2

    Average Daily Volume

    a5

    Ask Size

    b

    Bid

    b2

    Ask (Real-time)

    b3

    Bid (Real-time)

    b4

    Book Value

    b6

    Bid Size

    c

    Change & Percent Change

    c1

    Change

    c3

    Commission

    c6

    Change (Real-time)

    c8

    After Hours Change (Real-time)

    d

    Dividend/Share

    d1

    Last Trade Date

    d2

    Trade Date

    e

    Earnings/Share

    e1

    Error Indication (returned for symbol changed / invalid)

    e7

    EPS Estimate Current Year

    e8

    EPS Estimate Next Year

    e9

    EPS Estimate Next Quarter

    f6

    Float Shares

    g

    Day’s Low

    h

    Day’s High

    j

    52-week Low

    k

    52-week High

    g1

    Holdings Gain Percent

    g3

    Annualized Gain

    g4

    Holdings Gain

    g5

    Holdings Gain Percent (Real-time)

    g6

    Holdings Gain (Real-time)

    i

    More Info

    i5

    Order Book (Real-time)

    j1

    Market Capitalization

    j3

    Market Cap (Real-time)

    j4

    EBITDA

    j5

    Change From 52-week Low

    j6

    Percent Change From 52-week Low

    k1

    Last Trade (Real-time) With Time

    k2

    Change Percent (Real-time)

    k3

    Last Trade Size

    k4

    Change From 52-week High

    k5

    Percebt Change From 52-week High

    l

    Last Trade (With Time)

    l1

    Last Trade (Price Only)

    l2

    High Limit

    l3

    Low Limit

    m

    Day’s Range

    m2

    Day’s Range (Real-time)

    m3

    50-day Moving Average

    m4

    200-day Moving Average

    m5

    Change From 200-day Moving Average

    m6

    Percent Change From 200-day Moving Average

    m7

    Change From 50-day Moving Average

    m8

    Percent Change From 50-day Moving Average

    n

    Name

    n4

    Notes

    o

    Open

    p

    Previous Close

    p1

    Price Paid

    p2

    Change in Percent

    p5

    Price/Sales

    p6

    Price/Book

    q

    Ex-Dividend Date

    r

    P/E Ratio

    r1

    Dividend Pay Date

    r2

    P/E Ratio (Real-time)

    r5

    PEG Ratio

    r6

    Price/EPS Estimate Current Year

    r7

    Price/EPS Estimate Next Year

    s

    Symbol

    s1

    Shares Owned

    s7

    Short Ratio

    t1

    Last Trade Time

    t6

    Trade Links

    t7

    Ticker Trend

    t8

    1 yr Target Price

    v

    Volume

    v1

    Holdings Value

    v7

    Holdings Value (Real-time)

    w

    52-week Range

    w1

    Day’s Value Change

    w4

    Day’s Value Change (Real-time)

    x

    Stock Exchange

    python获取yahoo财经信息工具类

       ystockquote.py

    Python代码  
    1. #!/usr/bin/env python  
    2. #  
    3. #  Copyright (c) 2007-2008, Corey Goldberg (corey@goldb.org)  
    4. #  
    5. #  license: GNU LGPL  
    6. #  
    7. #  This library is free software; you can redistribute it and/or  
    8. #  modify it under the terms of the GNU Lesser General Public  
    9. #  License as published by the Free Software Foundation; either  
    10. #  version 2.1 of the License, or (at your option) any later version.  
    11.   
    12.   
    13. import urllib  
    14.   
    15.   
    16. """ 
    17. This is the "ystockquote" module. 
    18.  
    19. This module provides a Python API for retrieving stock data from Yahoo Finance. 
    20.  
    21. sample usage: 
    22. >>> import ystockquote 
    23. >>> print ystockquote.get_price('GOOG') 
    24. 529.46 
    25. """  
    26.   
    27.   
    28. def __request(symbol, stat):  
    29.     url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, stat)  
    30.     return urllib.urlopen(url).read().strip().strip('"')  
    31.   
    32.   
    33. def get_all(symbol):  
    34.     """ 
    35.     Get all available quote data for the given ticker symbol. 
    36.      
    37.     Returns a dictionary. 
    38.     """  
    39.     values = __request(symbol, 'l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7').split(',')  
    40.     data = {}  
    41.     data['price'] = values[0]  
    42.     data['change'] = values[1]  
    43.     data['volume'] = values[2]  
    44.     data['avg_daily_volume'] = values[3]  
    45.     data['stock_exchange'] = values[4]  
    46.     data['market_cap'] = values[5]  
    47.     data['book_value'] = values[6]  
    48.     data['ebitda'] = values[7]  
    49.     data['dividend_per_share'] = values[8]  
    50.     data['dividend_yield'] = values[9]  
    51.     data['earnings_per_share'] = values[10]  
    52.     data['52_week_high'] = values[11]  
    53.     data['52_week_low'] = values[12]  
    54.     data['50day_moving_avg'] = values[13]  
    55.     data['200day_moving_avg'] = values[14]  
    56.     data['price_earnings_ratio'] = values[15]  
    57.     data['price_earnings_growth_ratio'] = values[16]  
    58.     data['price_sales_ratio'] = values[17]  
    59.     data['price_book_ratio'] = values[18]  
    60.     data['short_ratio'] = values[19]  
    61.     return data  
    62.       
    63.       
    64. def get_price(symbol):   
    65.     return __request(symbol, 'l1')  
    66.   
    67.   
    68. def get_change(symbol):  
    69.     return __request(symbol, 'c1')  
    70.       
    71.       
    72. def get_volume(symbol):   
    73.     return __request(symbol, 'v')  
    74.   
    75.   
    76. def get_avg_daily_volume(symbol):   
    77.     return __request(symbol, 'a2')  
    78.       
    79.       
    80. def get_stock_exchange(symbol):   
    81.     return __request(symbol, 'x')  
    82.       
    83.       
    84. def get_market_cap(symbol):  
    85.     return __request(symbol, 'j1')  
    86.      
    87.      
    88. def get_book_value(symbol):  
    89.     return __request(symbol, 'b4')  
    90.   
    91.   
    92. def get_ebitda(symbol):   
    93.     return __request(symbol, 'j4')  
    94.       
    95.       
    96. def get_dividend_per_share(symbol):  
    97.     return __request(symbol, 'd')  
    98.   
    99.   
    100. def get_dividend_yield(symbol):   
    101.     return __request(symbol, 'y')  
    102.       
    103.       
    104. def get_earnings_per_share(symbol):   
    105.     return __request(symbol, 'e')  
    106.   
    107.   
    108. def get_52_week_high(symbol):   
    109.     return __request(symbol, 'k')  
    110.       
    111.       
    112. def get_52_week_low(symbol):   
    113.     return __request(symbol, 'j')  
    114.   
    115.   
    116. def get_50day_moving_avg(symbol):   
    117.     return __request(symbol, 'm3')  
    118.       
    119.       
    120. def get_200day_moving_avg(symbol):   
    121.     return __request(symbol, 'm4')  
    122.       
    123.       
    124. def get_price_earnings_ratio(symbol):   
    125.     return __request(symbol, 'r')  
    126.   
    127.   
    128. def get_price_earnings_growth_ratio(symbol):   
    129.     return __request(symbol, 'r5')  
    130.   
    131.   
    132. def get_price_sales_ratio(symbol):   
    133.     return __request(symbol, 'p5')  
    134.       
    135.       
    136. def get_price_book_ratio(symbol):   
    137.     return __request(symbol, 'p6')  
    138.          
    139.          
    140. def get_short_ratio(symbol):   
    141.     return __request(symbol, 's7')  
    142.       
    143.       
    144. def get_historical_prices(symbol, start_date, end_date):  
    145.     """ 
    146.     Get historical prices for the given ticker symbol. 
    147.     Date format is 'YYYYMMDD' 
    148.      
    149.     Returns a nested list. 
    150.     """  
    151.     url = 'http://ichart.yahoo.com/table.csv?s=%s&' % symbol +   
    152.           'd=%s&' % str(int(end_date[4:6]) - 1) +   
    153.           'e=%s&' % str(int(end_date[6:8])) +   
    154.           'f=%s&' % str(int(end_date[0:4])) +   
    155.           'g=d&' +   
    156.           'a=%s&' % str(int(start_date[4:6]) - 1) +   
    157.           'b=%s&' % str(int(start_date[6:8])) +   
    158.           'c=%s&' % str(int(start_date[0:4])) +   
    159.           'ignore=.csv'  
    160.     days = urllib.urlopen(url).readlines()  
    161.     data = [day[:-2].split(',') for day in days]  
    162.     return data  
    163.           

      使用工具类

    Python代码  
    1. import ystockquote  
    2. print ystockquote.get_price('600887.SS')  

    =======================

    和讯网股票查询接口
    2011-01-26 12:38

    最近想自己写个股票查询的小软件,网上找到了新浪的javascript接口,比较方便,但是新浪的接口没有把换手率等信息直接返回,刚好看到和讯网的接口有这个信息,所以记录下来。

    查询接口例:

    http://bdcjhq.hexun.com/quote?s2=000001.sh,399001.sz,399300.sz,601186.sh

    返回结果:

     <html><head><meta http-equiv="Content-Type" content="text/html; charset=GBK"><script type='text/javascript'>document.domain='hexun.com';</script></head><body></body><script>try{parent.bdcallback({"399001.sz":{na:"深证成指",pc:"11446.372",op:"11459.016",vo:"2193906",tu:"379028",hi:"11624.467",lo:"11459.016",la:"11558.021",type:"1",time:"2011-01-26 11:30:22"},"399300.sz":{na:"沪深300 ",pc:"2938.654",op:"2939.773",vo:"21205501",tu:"2559517",hi:"2971.763",lo:"2939.356",la:"2960.847",type:"1",time:"2011-01-26 11:31:07"},"601186.sh":{na:"中国铁建",pc:"7.22",op:"7.22",vo:"174035",tu:"12804",hi:"7.42",lo:"7.22",la:"7.39",type:"2",time:"2011-01-26 11:30:15",sy:"18.45",lt:"24.50",sz:"911.74",hs:"0.71",is:"0"},"tofnow":{time:"2011-01-26 11:37:51"}})}catch(e){}</script></html>
     

     从返回的结果看,股票数据应该就是JSON串,因此解析就比较方便了。

    各个字段的意义如下:

    "601186.sh":          //股票代码

    {

    na:"中国铁建",       //股票中文名称

    pc:"7.22",           //昨收盘

    op:"7.22",           //今开盘

    vo:"174035",         //成交量

    tu:"12804",          //成交额

    hi:"7.42",           //最高价

    lo:"7.22",           //最低价

    la:"7.39",           //现价

    type:"2",            //类型,1:指数,2:股票?

    time:"2011-01-26 11:30:15", //时间

    sy:"18.45",          //市盈率= 现价/最近四个季度摊薄每股收益之和

    lt:"24.50",          //流通股数(单位:亿股)

    sz:"911.74",         //总市值(单位:亿)

    hs:"0.71",           //换手率

    is:"0"               // ??暂时未知

    }

    Google 的数据

    1. 实时数据 http://www.google.com/ig/api?h1=zh-CN&stock=600001

    2.K线图 http://www.google.cn/finance/getchart?q=600336&x=SHA&p=5d&i=240 http://www.google.com/finance/chart?q=SHA:600001&amp;tlf=12

    3. 补全信息 "http://www.google.cn/finance/match?matchtype=matchall&q=600".

  • 相关阅读:
    android apk 防止反编译技术第四篇-对抗JD-GUI
    程序猿的健康之路
    webkit浏览器常见开发问题
    解密H264、AAC硬件解码的关键扩展数据处理
    HTTPS与证书
    【微软大法好】VS Tools for AI全攻略(3):低配置虚拟机也能玩转深度学习,无需NC/NV系列
    【微软大法好】VS Tools for AI全攻略(2)
    【微软大法好】VS Tools for AI全攻略
    【写论文历程】这几天的一个小总结
    不得不承认pretty-midi很好用,以及一些简单的上手
  • 原文地址:https://www.cnblogs.com/rockchip/p/3182525.html
Copyright © 2020-2023  润新知