【问题标题】:Historical stock data Error历史股票数据错误
【发布时间】:2023-03-10 13:29:02
【问题描述】:

当我尝试从雅虎或谷歌(我都尝试过)检索股票价格时,它不断返回此错误。我不知道这意味着什么或如何解决它。我以前用过这段代码,效果很好。你能帮我解决这个错误并解决它吗?谢谢。

import datetime as dt
import pandas as pd
import pandas_datareader.data as web


start = dt.datetime(2000,1,1)
end = dt.datetime(2004,1,1)

df= web.DataReader('TSLA', 'yahoo', start, end)
print(df.head)

ConnectionError: HTTPConnectionPool(host='ichart.finance.yahoo.com', port=80): 最大重试次数超出了 url: /table.csv?a=0&ignore=.csv&s=TSLA&b=1&e=1&d=0&g= d&f=2004&c=2000 (由NewConnectionError(': 无法建立新连接: [Errno 8] nodename nor servname provided, or not known',))

【问题讨论】:

  • 这个问题已经在这个网站上被问了 5-10 次。试试fix-yahoo-finance 包。

标签: python python-2.7


【解决方案1】:

只需将这部分:
df=web.DataReader("TSLA","yahoo",start,end) 更改为:df=web.DataReader("TSLA","google",start,end)

这里的问题在于雅虎搜索引擎。所以希望这能解决问题。

【讨论】:

  • 这也行不通,因为我想即使是谷歌也已经停止在其主网上导入股票数据。请检查。
【解决方案2】:

这是我找到的解决方法:

# Define the instruments to download. We would like to see Apple, Microsoft and the S&P500 index.
tickers = ['AAPL', 'MSFT', 'SPY']

# Define which online source one should use
data_source = 'google'

# We would like all available data from 01/01/2000 until 12/31/2016.
start_date = '2010-01-01'
end_date = '2016-12-31'

# User pandas_reader.data.DataReader to load the desired data. As simple as that.
panel_data = data.DataReader(tickers, data_source, 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 between 01/01/2000 and 12/31/2016
all_weekdays = pd.date_range(start=start_date, end=end_date, freq='B')

# How do we align the existing prices in adj_close with our 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)

来自http://www.learndatasci.com/python-finance-part-yahoo-finance-api-pandas-matplotlib/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 2010-10-19
    • 2016-08-13
    • 2021-09-19
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多