Get stocks data with python. 用python获得股票数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 导入第三方库
import pandas_datareader
import datetime
import matplotlib.pyplot as plt

# 获取现在的时间
# (2020, 3, 7, 10, 37, 13, 186885)
end = datetime.datetime.now()
start = datetime.datetime(end.year - 1,
end.month,
end.day)
# 获取阿里巴巴数据
alibaba = pandas_datareader.data.DataReader('BABA', 'yahoo', start, end)

# 打印数据
print(alibaba)

alibaba['Adj Close'].plot(legend = True,
figsize = (10, 4))
alibaba['Open'].plot(legend = True,
figsize = (10, 4))
alibaba['High'].plot(legend = True,
figsize = (10, 4))
# 把图片show出来
plt.show()

运行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
                  High         Low  ...    Volume   Adj Close
Date ...
2019-03-06 185.589996 183.020004 ... 10009100 184.169998
2019-03-07 181.800003 176.729996 ... 16488900 177.320007
2019-03-08 175.350006 171.565002 ... 14674200 175.029999
2019-03-11 181.720001 177.580002 ... 13764000 180.410004
2019-03-12 182.179993 179.509995 ... 8660000 180.630005
2019-03-13 182.535004 179.259995 ... 8868200 180.699997
2019-03-14 180.820007 178.009995 ... 9272000 180.360001
2019-03-15 181.449997 179.660004 ... 10844100 180.970001
2019-03-18 182.889999 180.759995 ... 7847800 181.830002
2019-03-19 183.360001 180.850006 ... 10238600 182.139999
2019-03-20 181.949997 178.429993 ... 15901500 181.279999
2019-03-21 181.729996 178.520004 ... 9754500 181.500000
2019-03-22 180.479996 175.335999 ... 11688400 176.259995
2019-03-25 178.899994 174.100006 ... 7511400 178.770004
2019-03-26 180.649994 177.095001 ... 7897900 178.080002
2019-03-27 179.830002 176.912994 ... 8371000 177.029999
2019-03-28 178.529999 175.970001 ... 7233000 177.729996
2019-03-29 182.600006 179.000000 ... 13850900 182.449997
2019-04-01 185.559998 180.889999 ... 12714800 180.889999
2019-04-02 183.563004 180.949997 ... 8021500 181.740005
2019-04-03 180.699997 176.759995 ... 26819000 178.320007
2019-04-04 181.960007 176.889999 ... 16993200 181.070007
2019-04-05 185.500000 182.000000 ... 18705000 185.350006
2019-04-08 187.820007 184.009995 ... 14725600 186.500000
2019-04-09 187.889999 186.160004 ... 11578200 187.190002
2019-04-10 187.399994 184.000000 ... 10655000 186.190002
2019-04-11 186.059998 183.750000 ... 8900300 184.979996
2019-04-12 189.789993 187.139999 ... 12600000 188.910004
2019-04-15 188.169998 182.559998 ... 14616600 183.070007
2019-04-16 185.789993 183.399994 ... 12195900 185.779999
... ... ... ... ... ...
2020-01-24 219.830002 211.324997 ... 18143900 213.750000
2020-01-27 208.020004 199.500000 ... 24574700 205.470001
2020-01-28 210.910004 207.169998 ... 16196700 210.229996
2020-01-29 213.979996 209.520004 ... 12871900 212.020004
2020-01-30 209.860001 205.029999 ... 14376000 208.580002
2020-01-31 207.929993 204.727997 ... 18181400 206.589996
2020-02-03 215.020004 208.669998 ... 14131900 213.100006
2020-02-04 224.380005 220.490005 ... 16695100 222.880005
2020-02-05 226.699997 217.539993 ... 15766100 220.220001
2020-02-06 223.649994 219.779999 ... 10790800 220.899994
2020-02-07 217.839996 214.880005 ... 13790300 216.529999
2020-02-10 215.770004 212.199997 ... 17420300 215.770004
2020-02-11 220.009995 215.289993 ... 16073500 217.210007
2020-02-12 225.520004 220.210007 ... 18671900 224.309998
2020-02-13 225.000000 218.990005 ... 28069700 220.360001
2020-02-14 221.639999 218.229996 ... 10690000 219.630005
2020-02-18 220.850006 217.509995 ... 12987000 220.520004
2020-02-19 223.559998 220.750000 ... 10798100 222.139999
2020-02-20 222.500000 214.220001 ... 14950100 218.039993
2020-02-21 217.600006 211.559998 ... 17681200 212.589996
2020-02-24 207.279999 202.509995 ... 19380200 206.160004
2020-02-25 209.949997 204.100006 ... 18132400 205.610001
2020-02-26 213.080002 206.789993 ... 19482100 208.740005
2020-02-27 209.970001 201.860001 ... 22741200 205.029999
2020-02-28 208.919998 198.561005 ... 31276200 208.000000
2020-03-02 211.110001 203.755997 ... 21542200 210.979996
2020-03-03 211.389999 202.240005 ... 20813800 207.410004
2020-03-04 212.699997 208.850006 ... 12474400 211.960007
2020-03-05 215.149994 209.139999 ... 13462900 211.460007
2020-03-06 207.000000 201.100006 ... 21679700 204.639999

[254 rows x 6 columns]