1
from selenium import webdriver

driver = webdriver.Chrome(executable_path="D:\chromedriver.exe")
#url = 'https://www.dcrustedp.in/show_chart.php'
driver.get('https://www.dcrustedp.in/show_chart.php')

rows = 2
cols = 5

for r in range(5,rows+1):
    for c in range(6,cols+1):
        value = driver.find_element_by_xpath("/html/body/center/table/tbody/tr["+str(r)+"]/td["+str(c)+"]").text
        print(value)

` This is my code. I want to extract result date of B.Tech - Computer Science and Engineering 5th Semester. It is in the first row of table. The date is 24-02-2020. I want to print the date from that particular cell only.

2
  • According to the your for loop of 'r', it starts from 5 and finishes on 3 (rows+1). Also same problem in 'c' loop as starts from 6 and finishes on 6 (cols+1). You need to change these intervals (rows+1, 6) and (cols+1,7). Commented Feb 26, 2020 at 23:45
  • find by xpath is actually a method from selenium. However, there is a library etree which can provide a similar functionality. You can refer to this link. Hope this helps. stackoverflow.com/questions/11465555/… Commented Feb 28, 2020 at 6:04

2 Answers 2

1

The below code works-:

from selenium import webdriver
from bs4 import BeautifulSoup
import time
webpage = 'https://www.dcrustedp.in/show_chart.php'
driver = webdriver.Chrome(executable_path='Your/path/to/chromedriver.exe') 
driver.get(webpage)
time.sleep(15)
html = driver.page_source

soup = BeautifulSoup(html, "html.parser")

pagehits=driver.find_element_by_xpath("/html/body/center/table/tbody/tr[3]/td[5]")
print(pagehits.text)

driver.quit()

Without Selenium, we can use requests library to fetch the table and then respective element

import requests
import pandas as pd
url = 'https://www.dcrustedp.in/show_chart.php'
html = requests.get(url, verify=False).content
df_list = pd.read_html(html)
df = df_list[-1]
print(df.iat[0,4])
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you Sir ! The code worked perfectly as per my requirement. Also, can you please help me to find a basic-simple code to just print the text from the pre-defined xpath. I don't want the chrome browser to open.
@GauravSekhri find by xpath is actually a method from selenium. However, there is a library etree which can provide a similar functionality. You can refer to this link. Hope this helps. stackoverflow.com/questions/11465555/…
Can you please make some changes in your code so that the web browser doesn't open each time I run the code?
@GauravSekhri I have made an edit to the original answer. Also please click upvote if the changes work for you!
Thanks a lot for helping me. Also, when I try to upvote your answer, a pop-up is displayed ("Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score.")
|
0

To extract the result date of 5th Semester for any of the Prg. Title, you have to induce WebDriverWait for the visibility_of_element_located() and you can use the following Locator Strategy:

  • xpath:

    driver.get('https://www.dcrustedp.in/show_chart.php')
    prg_title = "B.Tech - Computer Science and Engineering"
    # prg_title = "B.Tech - Electrical Engineering"
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[contains(., '"+prg_title+"')]//following-sibling::td[3]"))).get_attribute("innerHTML"))
    
  • Console Output:

    24-02-2020
    

Comments

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.