天天看點

TuShare(1):Python股票抓取工具TuShare

本文的原文連接配接是: http://blog.csdn.net/freewebsys/article/details/50781561 未經部落客允許不得轉載。

部落客位址是:http://blog.csdn.net/freewebsys

1,關于TuShare

http://tushare.org/

TuShare是一個免費、開源的python财經資料接口包。主要實作對股票等金融資料從資料采集、清洗加工 到 資料存儲的過程,能夠為金融分析人員提供快速、整潔、和多樣的便于分析的資料,為他們在資料擷取方面極大地減輕工作量,使他們更加專注于政策和模型的研究與實作上。考慮到Python pandas包在金融量化分析中展現出的優勢,TuShare傳回的絕大部分的資料格式都是pandas DataFrame類型,非常便于用pandas/NumPy/Matplotlib進行資料分析和可視化。當然,如果您習慣了用Excel或者關系型資料庫做分析,您也可以通過TuShare的資料存儲功能,将資料全部儲存到本地後進行分析。應一些使用者的請求,從0.2.5版本開始,TuShare同時相容Python 2.x和Python 3.x,對部分代碼進行了重構,并優化了一些算法,確定資料擷取的高效和穩定。

2,安裝TuShare

git clone https://github.com/waditu/tushare.git
cd tushare/
#首先安裝pandas和 lxml
yum install python-devel
yum install python-pip
pip install numpy --upgrade
pip install cycler --upgrade
pip install pandas
pip install lxml
pip install tushare
           

因為python的官網通路速度不是太好。多試驗幾次指令。

一般都是網絡問題,安裝的時候缺少那個包,就安裝那個包。

#########################################
  File "/usr/lib64/python2.6/site-packages/pandas/tools/plotting.py", line 137, in <module>

    import cycler

ImportError: No module named cycler
#########################################
比如缺少這個包,直接安裝就行。
pip install cycler --upgrade
           

3,安裝完成測試下

測試獲得股票資料。比如獲得股票 600848 的資料。

Python  (r266:, Jul  , ::) 
[GCC   (Red Hat -)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tushare as ts
>>> 
>>> ts.get_hist_data('600848') #一次性擷取全部資料
             open   high  close    low     volume  price_change  p_change  \
date                                                                        
--                              
--                              
--                    -     -   
--                    -     -   
--                    -     -   
--                              
--                    -     -   
--                              
--                              
--                    -     -   
--                              
--                              
--                    -     -   
--                              
--                              
--                    -     -   
--                              
--                              
--                              
--                    -     -   
--                    -     -   
--                    -     -   
--                              
--                              
--                    -     -   
--                    -     -   
--                              
--                              
--                    -     -   
--                              
...           ...    ...    ...    ...        ...           ...       ...   
--                                  
--                        -     -   
--                        -     -   
--                        -     -   
--                                  
--                       -     -   
--                                 
--                                  
--                        -     -   
--                                  
--                                  
--                        -     -   
--                                  
--                                  
--                        -     -   
--                                  
--                                  
--                                  
--                         -     -   
--                         -     -   
--                        -     -   
--                                  
--                                  
--                        -     -   
--                        -     -   
--                        -     -   
--                        -     -   
--                                  
--                                  
--                        -     -   

               ma5    ma10    ma20     v_ma5    v_ma10    v_ma20  turnover  
date                                                                        
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
--                    
...            ...     ...     ...       ...       ...       ...       ...  
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       
--                       

[ rows x  columns]
>>> 
           

獲得實時資料:

>>> df = ts.get_realtime_quotes('000581')
>>> df[['code','name','price','bid','ask','volume','amount','time']]
     code  name  price    bid    ask   volume        amount      time
    威孚高科            ::
>>> 
           

按日期獲得股票資料:

>>> df = ts.get_tick_data('600848',date='2015-01-09')
>>> df.head()
       time  price change  volume  amount type
  ::                      買盤
  ::       --      14   15708   賣盤
  ::                 賣盤
  ::    -              賣盤
  ::    -            賣盤
  ::       --       6    6750   賣盤
  ::               買盤
  ::       --      23   25783   賣盤
  ::    -            賣盤
  ::                中性盤
>>>
           

4,總結

本文的原文連接配接是: http://blog.csdn.net/freewebsys/article/details/50781561 未經部落客允許不得轉載。

部落客位址是:http://blog.csdn.net/freewebsys

安裝雖然麻煩一點,但是使用還是很友善的。

具體分析的話要使用pandas進行分析。

http://pandas.pydata.org/

10分鐘入門文檔。

http://pandas.pydata.org/pandas-docs/stable/10min.html

有兩本書可以看看。

TuShare(1):Python股票抓取工具TuShare

另外一邊寫的金融的東西比較多,看不懂。

TuShare(1):Python股票抓取工具TuShare

股票金融還得繼續學習。