0

Am trying to scrape a table from a website, however am not sure if am able to correctly refer to the appropriate class. Am attaching the screenshot and also the body extracted from BeautifulSoup. Am i look at this wrongly, please excuse me, am very new to web scraping.

I need to extract the table that is present in the circled highlight, however not sure to how to traverse there.

enter image description here enter image description here

2
  • Please provide a minimal reproducible example (MWE) and don't post images of your code. Looks like the desired data isn't there as the data is generated by javascript, which isn't supported by libraries like requests. Commented Aug 23, 2022 at 10:24
  • thanks @joni sorry for the inconvenience. Is there a way to pull data thats generated by javascript ? Seems i'll have to explore selenium instead of BeautifulSoup ? Commented Aug 23, 2022 at 10:29

1 Answer 1

1

The webpage is loaded by JavaScript. So you can use selenium with bs4.

An example with working solution:

import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
from selenium.webdriver.chrome.options import Options

webdriver_service = Service("./chromedriver") #Your chromedriver path
driver = webdriver.Chrome(service=webdriver_service)
url = 'https://web.sensibull.com/option-chain?expiry=2022-08-25&tradingsymbol=NIFTY'
driver.get(url)
driver.maximize_window()
time.sleep(8)

soup=BeautifulSoup(driver.page_source, 'lxml')

data = []
for row in soup.find_all('div',class_="rt-tr-group"):
    OI_change = row.select_one('div.rt-td:nth-child(1)').text
    OI_lakh =  row.select_one('div.rt-td:nth-child(2)').text
    LTP = row.select_one('div.rt-td:nth-child(3)').get_text(strip=True)

    data.append({
        'OI_change':OI_change,
        'OI_lakh':OI_lakh,
        'LTP':LTP
        })
    
df = pd.DataFrame(data)
print(df)

Output:

    OI_change OI_lakh      LTP
0          -     0.1    1354.200%
1      -7.0%     1.4  1429.20+11%
2          -     0.2   1354.65+8%
3      -3.3%     0.8  1332.75+11%
4     -25.0%     0.0   1109.80-4%
..       ...     ...          ...
56    -21.2%     1.1     0.85-62%
57     -2.3%    59.5     0.95-58%
58    -10.9%     0.6     0.75-63%
59    -33.2%     6.2     0.65-70%
60    -26.1%     0.3     0.60-71%

[61 rows x 3 columns]
Sign up to request clarification or add additional context in comments.

3 Comments

Hey thanks a lot @F.Hoque, Could you help me how this works, also how do i find my chrome driver path ?
Seems I'll first need to download the chromedriver from chromedriver.storage.googleapis.com/index.html ?
At first you have to update your chrome browser if needed because your chrome browser and chromwebdriver must be the same version. Keep the chromedriver using drag and drop and the script file in the same folder. Download driver according to your system:sites.google.com/chromium.org/driver

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.