使用pandas_datareader获取美股数据遇RemoteDataError求助
Why You're Getting the Error
The error pops up because Google Finance discontinued its historical data API years ago—so the 'google' data source in pandas_datareader is no longer functional. The URL your code tries to access (http://www.google.com/finance/historical?q=...) is completely inactive now, hence the RemoteDataError.
Quick Fix: Use yfinance Instead
yfinance is a well-maintained library built specifically to replace the defunct Google Finance data feed. Here's how to adjust your code (plus I fixed a typo: MFST → MSFT for Microsoft's ticker):
- First install yfinance via pip:
pip install yfinance
- Update your code to use yfinance:
import yfinance as yf from datetime import datetime tech_list = ['AAPL', 'GOOG', 'MSFT', 'AMZN'] end = datetime.now() start = datetime(end.year-1, end.month, end.day) # Fetch all stock data in one go (more efficient than looping) all_stock_data = yf.download(tech_list, start=start, end=end) # If you still want individual DataFrames stored in global variables (matching your original approach) for stock in tech_list: globals()[stock] = all_stock_data.xs(stock, level=1, axis=1)
Alternative: Stick with pandas_datareader (Using yfinance Adapter)
If you prefer keeping the pandas_datareader syntax, you can enable its yfinance adapter:
from pandas_datareader import data from datetime import datetime import yfinance as yf yf.pdr_override() # Lets pandas_datareader use yfinance under the hood tech_list = ['AAPL', 'GOOG', 'MSFT', 'AMZN'] end = datetime.now() start = datetime(end.year-1, end.month, end.day) for stock in tech_list: globals()[stock] = data.DataReader(stock, start=start, end=end)
Quick Tip
Always double-check stock tickers—MFST is not a valid ticker for Microsoft; the correct one is MSFT.
内容的提问来源于stack exchange,提问作者Nikhil

