Yahoo和Google API失效后,获取股票价格数据的替代方案及实现方法
Hey there, totally get the frustration—losing access to Yahoo and Google's old stock data APIs can throw a wrench in your projects. But don’t worry, there are plenty of reliable alternatives out there, and I’ll walk you through each one with practical implementation steps:
1. Alpha Vantage
This is one of the most popular free options around. It offers real-time and historical stock data, plus forex, crypto, and market sentiment tools. The free tier gives you up to 5 API calls per minute and 500 per day, which is more than enough for most small-scale projects.
How to implement:
- Sign up for a free account on their platform to grab your unique API key.
- Use a simple HTTP request to pull data. Here’s a quick Python example with
requests:
import requests API_KEY = "your_api_key_here" symbol = "AAPL" # Fetch latest global quote endpoint url = f"https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={symbol}&apikey={API_KEY}" response = requests.get(url) data = response.json() # Extract the latest price from nested JSON latest_price = data["Global Quote"]["05. price"] print(f"Current AAPL price: ${latest_price}")
Swap GLOBAL_QUOTE for functions like TIME_SERIES_DAILY if you need historical daily data.
2. IEX Cloud
IEX Cloud has a generous free tier (100,000 calls per month) and focuses on high-quality, real-time US stock data. They also offer detailed financial statements, news, and market analytics.
How to implement:
- Sign up for a free account to get your publishable token.
- Example script to pull the latest price:
import requests TOKEN = "your_publishable_token_here" symbol = "MSFT" # Endpoint for latest price only url = f"https://cloud.iexapis.com/stable/stock/{symbol}/quote/latestPrice?token={TOKEN}" response = requests.get(url) latest_price = response.text print(f"Current MSFT price: ${latest_price}")
Change the endpoint to /quote instead of /quote/latestPrice to get extra data like volume or daily high/low.
3. Finnhub
Finnhub is perfect for global stock data—covering US, European, and Asian markets with real-time quotes. The free tier allows 60 calls per minute and 1,000 per day.
How to implement:
- Register for free to get your API key.
- Python example to fetch current price:
import requests API_KEY = "your_api_key_here" symbol = "TSLA" url = f"https://finnhub.io/api/v1/quote?symbol={symbol}&token={API_KEY}" response = requests.get(url) data = response.json() # 'c' in the response stands for current price latest_price = data["c"] print(f"Current TSLA price: ${latest_price}")
4. Polygon.io
Polygon.io offers free access to real-time and historical US stock data, with 5 calls per minute and 10,000 per month on the free tier. They also provide detailed trade aggregates and market news.
How to implement:
- Sign up for a free account to get your API key.
- Example to pull the previous day’s close price:
import requests API_KEY = "your_api_key_here" symbol = "AMZN" url = f"https://api.polygon.io/v2/aggs/ticker/{symbol}/prev?adjusted=true&apiKey={API_KEY}" response = requests.get(url) data = response.json() latest_price = data["results"][0]["c"] print(f"Previous close AMZN price: ${latest_price}")
Bonus: Domestic Chinese Market Option (Tushare)
If you’re working with Chinese A-share data, Tushare is a fantastic free tool. It provides comprehensive historical and real-time data for Chinese stocks, funds, and indices.
How to implement:
- Sign up on their platform to get your token, then install the package:
pip install tushare - Example code to get the latest price of a Chinese stock:
import tushare as ts ts.set_token("your_token_here") pro = ts.pro_api() # Fetch daily data for Ping An Bank (600000.SH) df = pro.daily(ts_code="600000.SH", start_date="20240101", end_date="20240101") latest_price = df["close"].iloc[0] print(f"Current 600000.SH price: ¥{latest_price}")
Quick Pro Tips:
- Respect rate limits: All free tiers have caps—cache data locally where possible to avoid hitting limits.
- Handle errors: Add try/except blocks to your code to manage API downtime or invalid requests gracefully.
- Parse carefully: Most APIs return nested JSON, so double-check key names to avoid missing data.
内容的提问来源于stack exchange,提问作者Roberto Fernandez Andersson




