Open StephanAkkerman opened 6 months ago
Yahoo: (from https://stackoverflow.com/questions/76065035/yahoo-finance-v7-api-now-requiring-cookies-python)
import requests
apiBase = "https://query2.finance.yahoo.com"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64)"}
def getCredentials(
cookieUrl="https://fc.yahoo.com", crumbUrl=apiBase + "/v1/test/getcrumb"
):
cookie = requests.get(cookieUrl).cookies
crumb = requests.get(url=crumbUrl, cookies=cookie, headers=headers).text
return {"cookie": cookie, "crumb": crumb}
def quote(symbols, credentials):
url = apiBase + "/v7/finance/quote"
params = {"symbols": ",".join(symbols), "crumb": credentials["crumb"]}
response = requests.get(
url, params=params, cookies=credentials["cookie"], headers=headers
)
quotes = response.json()["quoteResponse"]["result"]
return quotes
credentials = getCredentials()
quotes = quote(["GOOG", "TSLA"], credentials)
print(quotes)
We could make it quicker because many libraries support querying multiple tickers at the same time: yahooquery:
tradingview_ta also supports it, ccxt might, coingecko?