3

I am doing trading in a real time website. Now I want to scrap it continuously to extract real time stock or currency data for calculations using selenium and python. How do I approach the same. The webpage data is getting updated continuously and shown in the attached image in highlighted colours. Every time the data changes I should be able to extract it and do some calculations in the code. Pls help me to achieve the same.

webpage I need to scrap

1 Answers1

1

to capture the dynamically changing value of Nifty50 for the 1D segment.

You should use the below XPath:

//button[text()='1D']/ancestor::nav//following-sibling::section/descendant::a[text()='Nifty 50']/../following-sibling::td/span

Your effective code would be:

driver.maximize_window()
wait = WebDriverWait(driver, 20)

driver.get('https://in.investing.com/')

i = 0
while True:
   time.sleep(5)
   oneD_Nifty50 = wait.until(EC.visibility_of_element_located((By.XPATH, "//button[text()='1D']/ancestor::nav//following-sibling::section/descendant::a[text()='Nifty 50']/../following-sibling::td/span")))
   print(oneD_Nifty50.text)
   i = i + 1

   if i == 10:
       break
   else:
       continue

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Output:

17,828.80
17,828.65
17,828.40
17,828.70
17,828.70
17,827.20
17,827.70
17,827.70
17,828.80
17,828.80

Process finished with exit code 0
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Thanks sir, but how to continuously fetch data and print. – Soumya Chakraborty Apr 06 '22 at 07:47
  • Updated above, you can choose any value for i where you want to come out from the loop. In this example I have just given `if i == 10:` you can try with 50 or 100 or any other number infact also I am retrieving data after 5 secs. – cruisepandey Apr 06 '22 at 07:54
  • 1
    Thanks sir for the help, I though the website will block the IP due to continuous pinging but it isn't. I will try the same in my trading website. – Soumya Chakraborty Apr 06 '22 at 08:01