Python-从大熊猫的Google Finance中撤出



我正在尝试使用pandas和pandas datareader从Google Finance中获取数据。这是我的代码:

#Importing libraries needed for pulls from Google
from pandas_datareader import data
import pandas as pd
import datetime
from datetime import date
#Define the instruments to download.  In this case: Apple, Microsoft, and 
the S&P500 index
tickers = ['APPL', 'MSFT', 'SPY']
start_date = datetime.datetime(2017, 12, 1)
end_date = datetime.datetime(2017, 12, 31)
#Use pandas_reader.data.DataReader to load the desired data
panel_data = data.DataReader('SPY', 'google', start_date, end_date)
#Getting just the adjusted closing prices.  This will return a Pandas DataFrame
#The index in this DataFrame is the major index of the panel_data.
close = panel_data.ix['Close']
#Getting all weekdays within date range.
all_weekdays = pd.date_range(start=start_date, end=end_date, freq='B')
#How do we align the existing prices in the adj_close with out new set of dates?
#All we need to do is reindex close using all_weekdays as the new index.
close = close.reindex(all_weekdays)
close.head(10)

这是控制台输出:

runfile('C:/Users/kjohn_000/.spyder-py3/temp.py', wdir='C:/Users/kjohn_000/.spyder-py3')
C:Userskjohn_000Anaconda3libsite-packagespandas_datareaderbase.py:201: SymbolWarning: Failed to read symbol: 
'APPL', replacing with NaN.
  warnings.warn(msg.format(sym), SymbolWarning)
C:Userskjohn_000Anaconda3libsite-
packagespandas_datareaderbase.py:201: SymbolWarning: Failed to read 
symbol: 'MSFT', replacing with NaN.
  warnings.warn(msg.format(sym), SymbolWarning)
C:Userskjohn_000Anaconda3libsite-packagespandas_datareaderbase.py:201: SymbolWarning: Failed to read symbol: 
'SPY', replacing with NaN.
  warnings.warn(msg.format(sym), SymbolWarning)
Traceback (most recent call last):
  File "<ipython-input-2-0ddd75de0396>", line 1, in <module>
    runfile('C:/Users/kjohn_000/.spyder-py3/temp.py', 
wdir='C:/Users/kjohn_000/.spyder-py3')
  File "C:Userskjohn_000Anaconda3libsite-packagesspyderutilssitesitecustomize.py", line 705, in runfile
    execfile(filename, namespace)
  File "C:Userskjohn_000Anaconda3libsite-
packagesspyderutilssitesitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
  File "C:/Users/kjohn_000/.spyder-py3/temp.py", line 14, in <module>
    panel_data = data.DataReader(tickers, dataSource, start_date, end_date)
  File "C:Userskjohn_000Anaconda3libsite-packagespandas_datareaderdata.py", line 137, in DataReader
session=session).read()
  File "C:Userskjohn_000Anaconda3libsite-
packagespandas_datareaderbase.py", line 186, in read
    df = self._dl_mult_symbols(self.symbols)
  File "C:Userskjohn_000Anaconda3libsite-
packagespandas_datareaderbase.py", line 206, in _dl_mult_symbols
    raise RemoteDataError(msg.format(self.__class__.__name__))
RemoteDataError: No data fetched using 'GoogleDailyReader'

为什么pandas datareader无法读取"股票"列表中的库存符号?我已经四处寻找答案几个小时,但是许多答案都是关于Yahoo API的回答问题,其余的答案要么是针对另一种语言,要么只是在编码时简直是我的深度(我相对较深(Python的新手(。预先感谢您的帮助和反馈。

这对我有用的python 3.6.1

from pandas_datareader import data
import fix_yahoo_finance as yf
yf.pdr_override() 
symbol = 'AMZN'
data_source='google'
start_date = '2010-01-01'
end_date = '2016-01-01'
df = data.get_data_yahoo(symbol, start_date, end_date)
print(df)
df.head()

这也对我有用。

from urllib.request import urlopen
from bs4 import BeautifulSoup as bs
def get_historical_data(name, number_of_days):
    data = []
    url = "https://finance.yahoo.com/quote/" + name + "/history/"
    rows = bs(urlopen(url).read()).findAll('table')[0].tbody.findAll('tr')
    for each_row in rows:
        divs = each_row.findAll('td')
        if divs[1].span.text  != 'Dividend': #Ignore this row in the table
            #I'm only interested in 'Open' price; For other values, play with divs[1 - 5]
            data.append({'Date': divs[0].span.text, 'Open': float(divs[1].span.text.replace(',',''))})
    return data[:number_of_days]
#Test
for i in get_historical_data('googl', 25):   
    print(i)

这不使用Google,但是,如果您使用Python Yahoofinancials模块,则可以轻松地将财务数据加载到Pandas中。Yahoofinancials通过散布相关的Yahoo Finance页面的数据存储对象来获取财务数据,因此它非常快,建造良好,不依赖旧的停用API或Web Driver,或者像Web Scraper一样。数据在JSON中返回。

$ pip安装yahoofinancials

用法示例:

from yahoofinancials import YahooFinancials
import pandas as pd
# Select Tickers and stock history dates
ticker = 'AAPL'
ticker2 = 'MSFT'
ticker3 = 'INTC'
index = '^NDX'
freq = 'daily'
start_date = '2012-10-01'
end_date = '2017-10-01'

# Function to clean data extracts
def clean_stock_data(stock_data_list):
    new_list = []
    for rec in stock_data_list:
        if 'type' not in rec.keys():
            new_list.append(rec)
    return new_list
# Construct yahoo financials objects for data extraction
aapl_financials = YahooFinancials(ticker)
mfst_financials = YahooFinancials(ticker2)
intl_financials = YahooFinancials(ticker3)
index_financials = YahooFinancials(index)
# Clean returned stock history data and remove dividend events from price history
daily_aapl_data = clean_stock_data(aapl_financials.get_historical_stock_data(start_date, end_date, freq)[ticker]['prices'])
daily_msft_data = clean_stock_data(mfst_financials.get_historical_stock_data(start_date, end_date, freq)[ticker2]['prices'])
daily_intl_data = clean_stock_data(intl_financials.get_historical_stock_data(start_date, end_date, freq)[ticker3]['prices'])
daily_index_data = index_financials.get_historical_stock_data(start_date, end_date, freq)[index]['prices']
stock_hist_data_list = [{'NDX': daily_index_data}, {'AAPL': daily_aapl_data}, {'MSFT': daily_msft_data}, {'INTL': daily_intl_data}]

# Function to construct data frame based on a stock and it's market index
def build_data_frame(data_list1, data_list2, data_list3, data_list4):
    data_dict = {}
    i = 0
    for list_item in data_list2:
        if 'type' not in list_item.keys():
            data_dict.update({list_item['formatted_date']: {'NDX': data_list1[i]['close'], 'AAPL': list_item['close'],
                                                            'MSFT': data_list3[i]['close'],
                                                            'INTL': data_list4[i]['close']}})
            i += 1
    tseries = pd.to_datetime(list(data_dict.keys()))
    df = pd.DataFrame(data=list(data_dict.values()), index=tseries,
                      columns=['NDX', 'AAPL', 'MSFT', 'INTL']).sort_index()
    return df

def股票(pape,dia(:纸= papeDIAS = DIA"从yf"中提取历史记录。url =&quot; https://query1.finance.yahoo.com/v7/finance/download/porload/" 纸 " .sa" "?"?&quot &quort ofere1 = 1597805534&amp; sterip2 = 1629341534&amp; Interval = 1d&amp; evestbase = paper &quot; sa.csv&quot;file = r'd: users repo 'file2 =文件 基础如果OS.PATH.EXISTS(file2(:OS.Remove(File2(别的:打印("不在这里"(wget.download(url(base2 = pd.read_csv(base(pd.options.display.max_rows = 14000#base2.tail(15(返回base2.tail(dias(

最新更新