Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I scrape a table with a known class name?

I want to scrape the table "Implied Volatility" from the following url ideally as a dataframe

https://optioncharts.io/options/AAPL/overview/option-statistics

I have identified the table with the classname "table border table-sm optioncharts-table-styling table-light"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<table class="table border table-sm optioncharts-table-styling table-light" style="">
            <tbody>
            <tr>
              <th>
                Implied Volatility
                <i class="bi bi-info-circle" rel="tooltip" data-bs-toggle="tooltip" data-bs-placement="right" title="" data-bs-original-title="The average implied volatility of options expring nearest to 30-days." aria-label="The average implied volatility of options expring nearest to 30-days."></i>
              </th>
              <td>17.49%</td>
            </tr>
...

So my idea was to use soup.find to search for the div and class name.
Both pandas and soup though fail to find the table.
I tried the code below which outputs a mix of random HTML and errors but not the table.

import requests
import pandas as pd
import json
from pandas.io.json import json_normalize
from bs4 import BeautifulSoup

url = 'https://optioncharts.io/options/AAPL/overview/option-statistics'
res = requests.get(url)
soup = BeautifulSoup(res.content, "lxml")

>Solution :

The data you see is loaded from external URL via Javascript. To load the data into pandas dataframe you can use:

import pandas as pd

api_url = "https://optioncharts.io/async/options_ticker_info?ticker=AAPL"

df = pd.read_html(api_url)[0].set_index(0).rename_axis(index=None)
print(df)

Prints:

                                        1
Implied Volatility                 17.49%
Historical Volatility              13.18%
IV Percentile                         12%
IV Rank                             9.07%
IV High                42.79% on 01/03/23
IV Low                 15.42% on 12/15/23

To get "Implied Volatility":

print(df.loc["Implied Volatility"])

Prints:

1    17.49%
Name: Implied Volatility, dtype: object
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading