美文网首页
从tushare获取数据

从tushare获取数据

作者: 1037号森林里一段干木头 | 来源:发表于2020-08-20 22:53 被阅读0次
    import tushare as ts
    ts.set_token('08733ac4361bb1a5137c02cb24a857b289e319432c3ec85726ce7987')
    pro=ts.pro_api()
    
    ts_code='601318.SH'
    start_date='20190701'
    end_date='20200810'
    

    以上方法只需要在第一次或者token失效后调用,完成调取tushare数据凭证的设置,
    正常情况下不需要重复设置。也可以忽略此步骤,直接用pro_api('your token')完成初始化

    import os
    current_path=os.getcwd()#current work dir
    
    csv_path_name=os.path.join(current_path,ts_code+".csv")
    
    daily = pro.daily(ts_code=ts_code, start_date=start_date, end_date=end_date)
    
    
    daily['open'][:5]#访问daily的方式,字典键值‘open’下的值是一个list
    
    0    76.02
    1    76.96
    2    77.00
    3    77.94
    4    76.81
    Name: open, dtype: float64
    
    type(daily)#返回的daily是pandas的dataframe类型
    
    pandas.core.frame.DataFrame
    
    daily[0:0]#显示daily的coloum name,pct_chg是股价涨跌百分数,vol:成交手数,amount:成交额
    
    
    ts_code trade_date  open    high    low close   pre_close   change  pct_chg vol amount
    

    daily#shift+tab 显示daily详细信息

    Type:        DataFrame
    String form:
    ts_code trade_date   open   high    low  close  pre_close  change  \
               0    601318.SH   2020 <...> 493   606496.56  5.535116e+06
               271   3.6452   926097.53  8.488850e+06
               
               [272 rows x 11 columns]
    Length:      272
    File:        ~/.local/lib/python3.7/site-packages/pandas/core/frame.py
    Docstring:  
    Two-dimensional, size-mutable, potentially heterogeneous tabular data.
    
    Data structure also contains labeled axes (rows and columns).
    Arithmetic operations align on both row and column labels. Can be
    thought of as a dict-like container for Series objects. The primary
    pandas data structure.
    
    Parameters
    ----------
    data : ndarray (structured or homogeneous), Iterable, dict, or DataFrame
        Dict can contain Series, arrays, constants, or list-like objects.
    
        .. versionchanged:: 0.23.0
           If data is a dict, column order follows insertion-order for
           Python 3.6 and later.
    
        .. versionchanged:: 0.25.0
           If data is a list of dicts, column order follows insertion-order
           for Python 3.6 and later.
    
    index : Index or array-like
        Index to use for resulting frame. Will default to RangeIndex if
        no indexing information part of input data and no index provided.
    columns : Index or array-like
        Column labels to use for resulting frame. Will default to
        RangeIndex (0, 1, 2, ..., n) if no column labels are provided.
    dtype : dtype, default None
        Data type to force. Only a single dtype is allowed. If None, infer.
    copy : bool, default False
        Copy data from inputs. Only affects DataFrame / 2d ndarray input.
    
    See Also
    --------
    DataFrame.from_records : Constructor from tuples, also record arrays.
    DataFrame.from_dict : From dicts of Series, arrays, or dicts.
    read_csv
    read_table
    read_clipboard
    
    Examples
    --------
    Constructing DataFrame from a dictionary.
    
    >>> d = {'col1': [1, 2], 'col2': [3, 4]}
    >>> df = pd.DataFrame(data=d)
    >>> df
       col1  col2
    0     1     3
    1     2     4
    
    Notice that the inferred dtype is int64.
    
    >>> df.dtypes
    col1    int64
    col2    int64
    dtype: object
    
    To enforce a single dtype:
    
    >>> df = pd.DataFrame(data=d, dtype=np.int8)
    >>> df.dtypes
    col1    int8
    col2    int8
    dtype: object
    
    Constructing DataFrame from numpy ndarray:
    
    >>> df2 = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
    ...                    columns=['a', 'b', 'c'])
    >>> df2
       a  b  c
    0  1  2  3
    1  4  5  6
    2  7  8  9
    
    moneyflow = pro.moneyflow(ts_code, start_date=start_date, end_date=end_date)
    moneyflow[0:0]#show coloum name
    
    ---------------------------------------------------------------------------
    
    Exception                                 Traceback (most recent call last)
    
    <ipython-input-25-5401f4deff29> in <module>
    ----> 1 moneyflow = pro.moneyflow(ts_code, start_date=start_date, end_date=end_date)
          2 moneyflow[0:0]#show coloum name
    
    
    ~/.conda/envs/py3/lib/python3.7/site-packages/tushare/pro/client.py in query(self, api_name, fields, **kwargs)
         42             result = json.loads(res.text)
         43             if result['code'] != 0:
    ---> 44                 raise Exception(result['msg'])
         45             data = result['data']
         46             columns = data['fields']
    
    
    Exception: 抱歉,您没有访问该接口的权限,权限的具体详情访问:https://tushare.pro/document/1?doc_id=108。
    

    积分不够没法访问........,本来想把开盘、收盘、资金流入、沪深港通资金等等的数据拿来描述股票每天的特征,但是看来用tushare行不通了。

    相关文章

      网友评论

          本文标题:从tushare获取数据

          本文链接:https://www.haomeiwen.com/subject/pwcyjktx.html