> ## Content Index
> Fetch the complete content index at: https://serpapi.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# Web scraping stock prices in Python
- URL: https://serpapi.com/blog/web-scraping-stock-prices-in-python/
- Published: 2024-08-29T02:06:34.000Z
- Updated: 2024-12-05T05:15:29.000Z
- Description: Learn how to scrape stock prices in Python using Google Finance API. Get finance data programmatically!
- Author: Hilman Ramadhan
- Tags: Google Finance

Ever wondered how to keep track of stock prices without constantly refreshing web pages? With Python, we can easily scrape stock price data and have all the information at our fingertips, all in real-time. Let’s dive into how you can do this with just a few lines of code!

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2024/08/scrape-stock-price-in-python-1.webp)

Web scraping stock prices in Python

## Scrape a Company’s Stock Data

If you need to monitor a company's stock, you can use this API: [Google Finance API](https://serpapi.com/google-finance-api). It scrapes data from the Google Finance page.

**Video Tutorial**  
If you prefer to watch a video, here it is

### Step 1: Tools 

We'll use the official Python library by SerpApi: [serpapi-python](https://github.com/serpapi/serpapi-python) .  
That's the only tool that we need!

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2023/10/tools-to-scrape-google-search-in-python.webp)

Python library from SerpApi, the tool we need to scrape Google SERP

> As a side note: You can use this library to scrape search results from other search engines, not just Google Finance.

Usually, you'll write your DIY solution using something like BeautifulSoup, Selenium, Scrapy, Requests, etc., to scrape Google Finance results. You can relax now since we perform all these heavy tasks for you. So, you don't need to worry about [all the problems you might've encountered while implementing your web scraping solution](https://serpapi.com/blog/how-to-reduce-chance-of-being-blocked-while-web/).

### Step 2: Setup and preparation

- Sign up for free at [SerpApi](https://serpapi.com/). You can get 100 free searches per month.
- Get your SerpApi Api Key from [your dashboard](https://serpapi.com/manage-api-key).
- Create a new `.env` file, and assign a new env variable with value from API\_KEY above.  
`SERPAPI_KEY=$YOUR_SERPAPI_KEY_HERE`
- Install python-dotenv to read the `.env` file with  
`pip install python-dotenv`
- Install SerpApi's Python library with  
`pip install serpapi`
- Create new `main.py` file for the main program.

Your folder structure will look like this:

```
|_ .env
|_ main.py

```

### Step 3: Write the code for scraping Google Finance results

Here is the code example for searching Tesla stock today:

```python
import os
import serpapi

from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv('SERPAPI_KEY')

client = serpapi.Client(api_key=api_key)
result = client.search(
	engine="google_finance",
	q="TSLA:NASDAQ",
	hl="en",
)

print(result)

```

Basic Google search result scrape with Python

Try to run this program with `python main.py` or `python3 main.py` from your terminal.

Let's compare the result from Google Finance page with the API response:

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2024/09/CleanShot-2024-09-04-at-06.14.03.png)

Result from Google finance page

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2024/09/CleanShot-2024-09-04-at-06.13.23.png)

Response sample result

As you can see, we've successfully gotten the data programmatically.

**Q parameter**  
We need the company's ticker name as value for the `q` parameter. You can find this by searching on Google. Feel free to change the value of the `q` parameter a ticker's code of a company.

**Filter by time**  
You can filter the results by time using the `window` parameter. Here are the options:  
`1D` \- 1 Day(default)  
`5D` \- 5 Days  
`1M` \- 1 Month  
`6M` \- 6 Months  
`YTD` \- Year to Date  
`1Y` \- 1 Year  
`5Y` \- 5 Years  
`MAX` \- Maximum

## **How to get Ticker name programmatically**

If you need to get the ticker name programmatically, there are several options:  
  
**Option A:**  
You can use our Google Search API using this keyword: "google.com/finance" {companyName}

Example on our playground:  
<https://serpapi.com/playground?q=%22google.com%2Ffinance%22+nvidia&location=Austin%2C+Texas%2C+United+States&gl=us&hl=en&google%5Fdomain=google.com&newPara=chips+stick+highlight+lr+as%5Fqdr+safe+google%5Fdomain+si+form+filter+as%5Fepq+client+votes+site+ie+oe+pws+ibp+source+as%5Fsitesearch+ijn+rlz+interval+size+color+maxPrice>

You can get the first item on the `organic_results` and cut the string from `link` or `displayed_link`.

**Option B:**  
You can use our Google Search API. The query will be "the company name" + " ticker name". Here is an example:

```python
company_name = "Tesla"
result = client.search(
	engine="google",
	q=company_name + " ticker name",
	hl="en",
)
```

The ticker name will be available on the response under `knowledge_graph > type`.

> Warning: There is no guarantee that it will work correctly for all the companies.

We also have a plan to support Google Finance Autocomplete in the future. Here is the related thread on our public roadmap: <https://github.com/serpapi/public-roadmap/issues/1905>

Interested in a no-code solution? Learn how to scrape Google finance reports using Google Sheets:

[Scrape Google Finance Reports with Google SheetsHow to scrape financial statements from Google Finance with Google Sheets using SerpApi’s Google Finance API.![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/size/w256h256/2021/07/serpapi-favicon.png)SerpApiAlex Barron![](https://images.unsplash.com/photo-1632507127789-eb70cc8757af?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wxMTc3M3wwfDF8c2VhcmNofDJ8fHN0b2NrJTIwdGlja2VyfGVufDB8fHx8MTcxOTIyODkxNXww&ixlib=rb-4.0.3&q=80&w=2000)](https://serpapi.com/blog/scraping-google-finance-statements-into-google-sheets/)

## Scrape Market Trends

SerpApi also provides the API for current market trends from this page: <https://www.google.com/finance/markets/indexes?hl=en>. We can use Google [Finance Markets API](https://serpapi.com/google-finance-markets) for this.

**Code sample**  
We can use the same setup as before with a few tweaks to some values:

```python
result = client.search(
    engine="google_finance_markets",
    trend="indexes",
    hl="en"
)
```

- Update engine to google\_finance\_markets
- add `trend` parameter

Here is the result:

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2024/08/CleanShot-2024-08-28-at-13.51.43@2x.png)

Google finance markets API result

Which represents this page:

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2024/08/CleanShot-2024-08-28-at-13.52.19@2x.png)

**Trend parameter**  
We can change the ranking factor using `trend` parameter. This parameter is used to retrieve different market trends. Available options:   
\- indexes - Market indexes   
\- most-active - Most active   
\- gainers - Gainers   
\- losers - Losers   
\- climate-leaders - Climate leaders   
\- cryptocurrencies - Crypto   
\- currencies - Currencies

I hope this is helpful! I hope it can be useful for your finance app project!