How can I get stock quotes using Google Finance API?

Viewed 315229

I'm looking for access to financial data from Google services.

I found this URL that gets the stock data for Microsoft.

What are all the possible parameters that Google allows for this kind of HTTP request? I'd like to see all the different information that I could get.

15 Answers

There's a whole API for managing portfolios. *Link removed. Google no longer provides a developer API for this.

Getting stock quotes is a little harder. I found one article where someone got stock quotes using Google Spreadsheets.

You can also use the gadgets but I guess that's not what you're after.

The API you mention is interesting but doesn't seem to be documented (as far as I've been able to find anyway).

Here is some information on historical prices, just for reference sake.

You can also pull data from Google Fiance directly in Google Sheets via GOOGLEFINANCE() function for both current and historical data:

GOOGLEFINANCE("NASDAQ:GOOGL", "price", DATE(2014,1,1), DATE(2014,12,31), "DAILY")

Another way is to use Yahoo finance instead via yfinance package. Or with such query which will return a JSON:

https://query1.finance.yahoo.com/v8/finance/chart/MSFT

Code to parse price and panel on the right, and example in the online IDE:

from bs4 import BeautifulSoup
import requests, lxml, json
from itertools import zip_longest


def scrape_google_finance(ticker: str):
    # https://docs.python-requests.org/en/master/user/quickstart/#passing-parameters-in-urls
    params = {
        "hl": "en"
        }

    # https://docs.python-requests.org/en/master/user/quickstart/#custom-headers
    # https://www.whatismybrowser.com/detect/what-is-my-user-agent
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36",
        }

    html = requests.get(f"https://www.google.com/finance/quote/{ticker}", params=params, headers=headers, timeout=30)
    soup = BeautifulSoup(html.text, "lxml")
    
    # describe empty dict where data will be appended
    ticker_data = {
        "ticker_data": {},
        "about_panel": {}
    }
    
    ticker_data["ticker_data"]["current_price"] = soup.select_one(".AHmHk .fxKbKc").text
    ticker_data["ticker_data"]["quote"] = soup.select_one(".PdOqHc").text.replace(" • ",":")
    ticker_data["ticker_data"]["title"] = soup.select_one(".zzDege").text
    
    right_panel_keys = soup.select(".gyFHrc .mfs7Fc")
    right_panel_values = soup.select(".gyFHrc .P6K39c")
    
    for key, value in zip_longest(right_panel_keys, right_panel_values):
        key_value = key.text.lower().replace(" ", "_")

        ticker_data["about_panel"][key_value] = value.text
    
    return ticker_data
    

data = scrape_google_finance(ticker="GOOGL:NASDAQ")

print(json.dumps(data, indent=2))

JSON output:

{
  "ticker_data": {
    "current_price": "$2,534.60",
    "quote": "GOOGL:NASDAQ",
    "title": "Alphabet Inc Class A"
  },
  "about_panel": {
    "previous_close": "$2,597.88",
    "day_range": "$2,532.02 - $2,609.59",
    "year_range": "$2,193.62 - $3,030.93",
    "market_cap": "1.68T USD",
    "volume": "1.56M",
    "p/e_ratio": "22.59",
    "dividend_yield": "-",
    "primary_exchange": "NASDAQ",
    "ceo": "Sundar Pichai",
    "founded": "Oct 2, 2015",
    "headquarters": "Mountain View, CaliforniaUnited States",
    "website": "abc.xyz",
    "employees": "156,500"
  }
}

Out of scope of your question. If there's a need to parse the whole Google Finance Ticker page, there's a line-by-line scrape Google Finance Ticker Quote Data in Python blog post about it at SerpApi.

I have personally built an app for stock data and fundamentals with Intrinio Two years ago but abandoned the project because I was beaten to market by a competitor.

I built it in Java but they support multiple stacks. Back then, You could access their api for free for testing purposes, but I think they build packages based on your needs now.

In any case, they were exceptionally helpful and charge low fees from what I remember, and their library is well documented so pulling data in json is very straightforward.

Related