I am trying to scrape data from tradingviews charts. This is the code that I am using and so far is working
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time
MAMAarray = []
FAMAarray = []
# example option: add 'incognito' command line arg to options
option = webdriver.ChromeOptions()
option.add_argument("--incognito")
# create new instance of chrome in incognito mode
browser = webdriver.Chrome(chrome_options=option)
# go to website of interest
browser.get("https://www.tradingview.com/chart/vKzVQllW/#")
# wait up to 10 seconds for page to load
timeout = 10
try:
WebDriverWait(browser, timeout).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[1]")))
except TimeoutException:
print("Timed out waiting for page to load")
browser.quit()
time.sleep(5)
# get MAMA and FAMA values for BTCUSDT on binance
MAMA_element = browser.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div/table/tbody/tr[1]/td[2]/div/div[3]/div[3]/div/span[3]")
FAMA_element = browser.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div/table/tbody/tr[1]/td[2]/div/div[3]/div[3]/div/span[4]")
MAMAarray.append(float(MAMA_element.text))
FAMAarray.append(float(FAMA_element.text))
print(MAMAarray)
print(FAMAarray)
while True:
MAMA_element = browser.find_element_by_xpath(
"/html/body/div[1]/div[1]/div/div[1]/div/table/tbody/tr[1]/td[2]/div/div[3]/div[3]/div/span[3]")
FAMA_element = browser.find_element_by_xpath(
"/html/body/div[1]/div[1]/div/div[1]/div/table/tbody/tr[1]/td[2]/div/div[3]/div[3]/div/span[4]")
if (float(MAMA_element.text)) != MAMAarray[-1]:
MAMAarray.append(float(MAMA_element.text))
if (float(FAMA_element.text)) != FAMAarray[-1]:
FAMAarray.append(float(FAMA_element.text))
print(MAMAarray)
print(FAMAarray)
Here is the output: so you can see that the numbers append but ONLY when I go into the chart manually and move my cursor over new candles(bars)
[7415.969]
[7417.39]
[7415.969, 7428.644]
[7417.39, 7435.585]
[7415.969, 7428.644, 7430.56]
[7417.39, 7435.585, 7431.722]
[7415.969, 7428.644, 7430.56, 7415.496]
[7417.39, 7435.585, 7431.722, 7417.39]
[7415.969, 7428.644, 7430.56, 7415.496, 7415.969]
[7417.39, 7435.585, 7431.722, 7417.39]
[7415.969, 7428.644, 7430.56, 7415.496, 7415.969]
[7417.39, 7435.585, 7431.722, 7417.39]
[7415.969, 7428.644, 7430.56, 7415.496, 7415.969]
[7417.39, 7435.585, 7431.722, 7417.39]
[7415.969, 7428.644, 7430.56, 7415.496, 7415.969]
[7417.39, 7435.585, 7431.722, 7417.39, 7424.887]
[7415.969, 7428.644, 7430.56, 7415.496, 7415.969, 7439.161]
[7417.39, 7435.585, 7431.722, 7417.39, 7424.887, 7424.409]
How do I go about automating this so that it automatically gets the newest value instead of me having to cursor over the new bar for it to get the new value
EDIT:
This is the value Im looking for Like I said I can get it to output through the script, but I cant get the value to update when a new candle is made