0

我正在尝试创建一个程序,当用户提供代码、开始日期和结束日期时从雅虎财经获取数据。您如何从 yahoo Finance 获取特定用户输入的股票代码的数据?

我尝试了以下方法,但这不起作用:

base_url = "http://ichart.finance.yahoo.com/"

def make_url(ticker_symbol,start_date, end_date):
print ticker_symbol
a = start_date
b = end_date
dt_url = '%s&a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&g=d&ignore=.csv'% (ticker_symbol, a.month-1, a.day, a.year, b.month-1, b.day,b.year)
return base_url + dt_url
4

2 回答 2

1

我认为最好的方法是使用“熊猫”。

from pandas.io.data import DataReader
data = DataReader('AAPL','yahoo',start = '1950-1-1', end = '2013-12-1')

数据对象是一个非常易于操作和使用的 pandas DataFrame

于 2013-12-13T22:07:17.433 回答
0

我推荐pandas_datareader。检查此链接: http: //pandas-datareader.readthedocs.io/en/stable/remote_data.html#yahoo-finance

这是我用来回答您的问题的代码:

[python3] C:\OneDrive\Todd\projects\Miniconda2\Scripts>python
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> from pandas_datareader import data
>>> from datetime import datetime
>>> def get_stock_data():
...   symbol = input("Stock symbol: ");
...   start_date_str = input("Start date (mm/dd/yyyy): ");
...   end_date_str = input("End date (mm/dd/yyyy): ");
...   start_date = datetime.strptime(start_date_str,"%m/%d/%Y");
...   end_date = datetime.strptime(end_date_str,"%m/%d/%Y");
...   df = data.DataReader(symbol, 'yahoo', start_date, end_date);
...   return(df);
...
>>> dis=get_stock_data();
Stock symbol: dis
Start date (mm/dd/yyyy): 08/01/2016
End date (mm/dd/yyyy): 08/31/2016
>>> dis.head(2)
                 Open       High        Low      Close    Volume  Adj Close
Date
2016-08-01  96.150002  96.199997  95.080002  95.540001   7131600  95.540001
2016-08-02  95.349998  95.750000  94.559998  95.010002   7644100  95.010002
>>> dis.tail(2)
                 Open       High        Low      Close   Volume  Adj Close
Date
2016-08-30  94.769997  95.290001  94.769997  94.860001  6845000  94.860001
2016-08-31  94.690002  94.900002  94.160004  94.459999  7375200  94.459999
>>> spy=get_stock_data()
Stock symbol: spy
Start date (mm/dd/yyyy): 08/01/2016
End date (mm/dd/yyyy): 08/31/2016
>>> spy.head(5)
                  Open        High         Low       Close    Volume  \
Date
2016-08-01  217.190002  217.649994  216.410004  216.940002  73311400
2016-08-02  216.649994  216.830002  214.570007  215.550003  92295500
2016-08-03  215.479996  216.250000  215.130005  216.179993  53993600
2016-08-04  216.309998  216.779999  214.250000  216.410004  46585500
2016-08-05  216.410004  218.229996  216.410004  218.179993  71892200

             Adj Close
Date
2016-08-01  216.940002
2016-08-02  215.550003
2016-08-03  216.179993
2016-08-04  216.410004
2016-08-05  218.179993
>>> spy.tail(5)
                  Open        High         Low       Close     Volume  \
Date
2016-08-25  217.399994  218.190002  217.220001  217.699997   69224800
2016-08-26  217.919998  219.119995  216.250000  217.289993  122506300
2016-08-29  217.440002  218.669998  217.399994  218.360001   70502200
2016-08-30  218.259995  218.589996  217.350006  218.000000   58114500
2016-08-31  217.610001  217.750000  216.470001  217.380005   85269500

             Adj Close
Date
2016-08-25  217.699997
2016-08-26  217.289993
2016-08-29  218.360001
2016-08-30  218.000000
2016-08-31  217.380005
>>> exit()
[python3] C:\OneDrive\Todd\projects\Miniconda2\Scripts>

如果上面的代码不起作用,您可能需要安装一些模块:

#Installing Anaconda: https://docs.continuum.io/anaconda/install#anaconda-for-windows-install
#Installing Python 3.3 on Anaconda Python for Windows:  http://www.walkingrandomly.com/?p=5089
## NOTE: I named my python 3 environment "python3", not "py3", so my commands vary slightly from those at the website above

#Installing pandas:  conda install -c https://conda.anaconda.org/anaconda pandas
#Installing datareader:  conda install -c https://conda.anaconda.org/anaconda pandas-datareader

安装完所有内容后,您可以通过以下方式确认一切正常:

C:\OneDrive\Todd\projects\Miniconda2\Scripts>activate python3
Deactivating environment "C:\OneDrive\Todd\projects\Miniconda2"...
Activating environment "C:\OneDrive\Todd\projects\Miniconda2\envs\python3"...

[python3] C:\OneDrive\Todd\projects\Miniconda2\Scripts>python
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>help('module pandas_datareader')  #Confirm the module is installed

Here is a list of modules whose name or summary contains 'pandas_datareader'.
If there are any, enter a module name to get more help.

...<several items listed, including...>
pandas_datareader.data - Module contains tools for collecting data from various remote sources
pandas_datareader.yahoo
pandas_datareader.yahoo.actions
pandas_datareader.yahoo.components
pandas_datareader.yahoo.daily
pandas_datareader.yahoo.options
pandas_datareader.yahoo.quotes

现在您已准备好从 Yahoo! 获取股票市场数据!金融。

于 2016-09-05T17:21:28.877 回答