> ## 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.

# Reverse engineering Google Finance charts
- URL: https://serpapi.com/blog/reverse-engineering-google-finance-charts/
- Published: 2021-12-03T13:21:51.000Z
- Updated: 2023-05-15T20:06:11.000Z
- Author: Alaa Abdulridha
- Tags: Google Finance, Web Scraping, Google, Tips&Tricks

In this article, we will talk about how to reverse engineer [Google finance](https://www.google.com/finance/) charts to parse them using Ruby on Rails.

##  Introduction

  
 When you search on Google for something like `Bitcoin price` or `bitcoin vs dollar` we will notice a chart and very rich finance data, the original source of this data is [Google finance](https://www.google.com/finance/).  
  
  
[What is Google finance?](https://en.wikipedia.org/wiki/Google%5FFinance)  
  
[Google Finance](https://www.google.com/finance/) is a website focusing on business news and financial information hosted by Google.

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2021/12/image-2.png)

Google finance data & chart "bitcoin vs dollar" - SerpApi

## **Getting started**

  
**W**e will ignore all the other data and we will focus on parsing the chart only as the extract of the other elements have been covered by other [SerpApi blog posts](https://serpapi.com/blog/).  
  
**B**asically, every chart or graph consists of two important parts (**x-axis* and *y-axis**).  
  
**T**he ***x*\-axis** is a horizontal line and the ***y*\-axis** is a vertical line.  
  
![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2021/12/image-8.png)

Coordinate grid y-axis and x-axis chart - SerpApi

Now we just need to understand the numbers in the Google finance chart, The **y-axis** represents the price column, and the **x-axis** represents the time.

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2021/12/image-9.png)

y-axis and x-axis represents the price and time - SerpApi

It's obvious now in the screenshot above, the price is **56,854.90** at **8:05.**  
  
Now we will find the chart CSS class:  

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2021/12/image-10.png)

Chart CSS selector - SerpApi

In this example, we will take the attribute,`jsdata` but we should note that the input of this attribute is changing every search.  
  
So by using REGEX we will extract the last element inside `jsdata="Wplt6c;_;AWRM64"` which means the element we want is `AWRM64`

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2021/12/image-11.png)

Using regex to extract chart data - SerpApi

**1-** This is the Regex that we used to search in the page source for the chart data.  
**2-** Is the raw HTML page - to search inside of it.  
**3-** The result, which it's the group of the chart JSON data.

After formatting the JSON, now we need to understand what's inside the JSON carefully.

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2021/12/image-12.png)

JSON chart data - SerpApi

**1-** This number represents the price.  
**2-** This one represents the time `in minutes`.  

## Putting everything together

  
Now we have to use `[dig](https://apidock.com/ruby/Hash/dig)` method to extract the JSON data we need, which consists of the x-axis and y-axis arrays (price and time).  
  
Then we will convert the time from minutes to hours, and we will use this formula to get the [UNIX time](https://www.unixtimestamp.com/index.php):

*`unix_time = time  60`*

and the last thing to do is convert the unix\_time to DateTime ( *`UTC Y-M-D:H:M` )*  

```ruby
data[:time] = Time.at(time * 60).utc.strftime("%Y-%m-%d %H:%M %p")

```

  
**The final result:**

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2021/12/image-13.png)

serpapi.com google finance chart scrapping

## **Ending:**

You can find the documentation about [how to use serpapi](https://serpapi.com/search-api) and you can follow us on Twitter at [@serp\_api](https://twitter.com/serp%5Fapi), to get our latest news and articles.