Search This Blog

Thursday, April 14, 2022

Extract Table Data Using Python

This is a very simple example. But you will know the dynamics to alter for achieving the right results. 

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from import By
from import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)

# Make Python sleep for some time

rows = len(driver.find_elements("xpath","/html/body/div[7]/div[1]/div[1]/div[3]/div/table/tbody/tr"))

# Obtain the number of columns in table
cols = len(driver.find_elements("xpath","/html/body/div[7]/div[1]/div[1]/div[3]/div/table/tbody/tr/td"))

# Printing the data of the table
for r in range(2, rows+1):
for p in range(1,4):
#obtaining the text from each column of the table
value = driver.find_element("xpath","/html/body/div[7]/div[1]/div[1]/div[3]/div/table/tbody/tr["+str(r)+"]/td["+str(p)+"]").text

No comments:

Post a Comment