• python2自动读取股票数据示例


    一、所需模块pandas_datareader的安装

    (python2) [root@DL ~]# pip install pandas_datareader     #用conda install panda_datareader提示找不到软件包,故改为pip install。

    ...

    Installing collected packages: lxml, idna, chardet, urllib3, requests, pandas-datareader
    Successfully installed chardet-3.0.4 idna-2.9 lxml-4.5.1 pandas-datareader-0.8.1 requests-2.24.0 urllib3-1.25.9

    二、示例

    import numpy as np
    import pandas as pd
    import pandas_datareader.data as web
    import datetime

    df_csvsave = web.DataReader("000858.SZ", "yahoo",datetime.datetime(2020,1,1),datetime.date.today())

    #说明:"000858.SZ",若是上市股票,后缀为SS,深市股票(包括创业版、中小板),后缀为SZ。“yahoo”,表示从yahoo网获取数据来源。

    print df_csvsave

    结果:

                         High           Low              Open            Close          Volume        Adj Close
    Date
    2020-01-02 133.500000 129.589996 132.000000 132.080002 30667439.0 130.3258514404297
    2020-01-03 132.070007 129.610001 131.600006 130.550003 20469248.0 128.81617736816406
    2020-01-06 130.250000 128.520004 130.000000 129.199997 25936979.0 127.48409271240234

    三、数据存为csv文件

    df_csvsave.to_csv('/root/d2l-zh/ccl_learning/000858.csv',columns=df_csvsave.columns,index=True)

    000858.csv内容:

    Date,High,Low,Open,Close,Volume,Adj Close
    2020-01-02,133.5,129.58999633789062,132.0,132.0800018310547,30667439.0,130.3258514404297
    2020-01-03,132.07000732421875,129.61000061035156,131.60000610351562,130.5500030517578,20469248.0,128.81617736816406
    2020-01-06,130.25,128.52000427246094,130.0,129.1999969482422,25936979.0,127.48409271240234
    2020-01-07,131.07000732421875,129.0,129.5,129.3699951171875,22327793.0,127.6518325805664
    2020-01-08,129.75999450683594,128.0500030517578,128.99000549316406,128.88999938964844,16180218.0,127.17821502685547

  • 相关阅读:
    Python 函数基础4 迭代器、生成器、枚举类型
    Python函数基础3 函数对象、名称空间、装饰器
    Python 函数基础2 实参与形参
    Python 函数基础1 定义、分类与嵌套使用
    Python 文件操作
    Python 字符编码、文件操作1
    Python 元组、字典、集合
    python 数据类型及内置方法
    Python 流程控制
    Python 基本数据类型、运算符
  • 原文地址:https://www.cnblogs.com/sfccl/p/13178524.html
Copyright © 2020-2023  润新知