[python] 获取股票信息

安装模块的时候,经常会报网络超时, 可以 -i: 指定库的安装源

pip install pandas_datareader  -i https://pypi.douban.com/simple 
pip install datetime  -i https://pypi.douban.com/simple

永久修改安装源,因为中间没有墙,所以安装模块会变得飞快

pip install -i http://pypi.douban.com/simple/ mrq --trusted-host pypi.douban.com
  1. 从一个简单的小栗子开始
# 基本信息
from datetime import datetime
# 股票数据读取
import pandas_datareader as pdr
# 时间
start = datetime(2020, 7, 10)

alibaba = pdr.get_data_yahoo("BABA", start=start)
print(alibaba.head())
print("--------------------------------------------------------")
amazon = pdr.get_data_yahoo("AMZN", start=start)
print(amazon.head())

print("info--------------------------------------------------------")
# 看看数据基本信息
alibaba.info()
print("describe--------------------------------------------------------")
print(alibaba.describe())
  1. 程序输出, 20200710左右五天股票数据
PS D:\python_fin> & E:/Programs/Python/Python38/python.exe d:/python_fin/get-stock.py
                  High         Low        Open       Close    Volume   Adj Close
Date
2020-07-09  268.000000  255.910004  265.549988  261.579987  36803900  261.579987
2020-07-10  261.899994  255.710007  260.209991  261.010010  20335000  261.010010
2020-07-13  265.660004  250.679993  261.450012  251.669998  21568700  251.669998
2020-07-14  249.750000  241.229996  246.570007  248.580002  19782600  248.580002
2020-07-15  252.500000  246.539993  251.479996  249.210007  12614700  249.210007
--------------------------------------------------------
                   High          Low         Open        Close   Volume    Adj Close
Date
2020-07-09  3193.879883  3074.000000  3115.989990  3182.629883  6388700  3182.629883
2020-07-10  3215.000000  3135.699951  3191.760010  3200.000000  5486000  3200.000000
2020-07-13  3344.290039  3068.389893  3251.060059  3104.000000  7720400  3104.000000
2020-07-14  3127.379883  2950.000000  3089.000000  3084.000000  7231900  3084.000000
2020-07-15  3098.350098  2973.179932  3080.229980  3008.870117  5788900  3008.870117
info--------------------------------------------------------
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 9 entries, 2020-07-09 to 2020-07-21
Data columns (total 6 columns):
 #   Column     Non-Null Count  Dtype
---  ------     --------------  -----
 0   High       9 non-null      float64
 1   Low        9 non-null      float64
 2   Open       9 non-null      float64
 3   Close      9 non-null      float64
 4   Volume     9 non-null      int64
 5   Adj Close  9 non-null      float64
dtypes: float64(5), int64(1)
memory usage: 504.0 bytes
describe--------------------------------------------------------
             High         Low        Open       Close        Volume   Adj Close
count    9.000000    9.000000    9.000000    9.000000  9.000000e+00    9.000000
mean   256.707779  249.359997  254.582223  252.681668  1.972020e+07  252.681668
std      8.228564    6.303035    8.284285    6.537228  7.362351e+06    6.537228
min    245.050003  240.740005  241.770004  242.500000  1.060031e+07  242.500000
25%    249.750000  244.479996  246.570007  248.580002  1.803210e+07  248.580002
50%    257.670013  250.679993  256.450012  251.669998  1.900210e+07  251.669998
75%    261.910004  255.710007  261.209991  257.635010  2.033500e+07  257.635010
max    268.000000  256.519989  265.549988  261.579987  3.680390e+07  261.579987

这么简单就搞定了,我心里还真有点小激动。
明天试着画个折线图。

代码参考: https://blog.csdn.net/qq_37635049/article/details/82012135

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值