I am trying to scrape the List of Trades from this TradingView chart(https://www.tradingview.com/chart/lUsimB6z/) but have no idea on where to start. I tried Selenium first but the table loads when you scroll down, which caused a problem for me. I am new to Selenium and Python in general. Any help will be appreciated. Thank you.
-
The link says 'access denied', I guess one needs to login to view the table you're talking about. – tidakdiinginkan Apr 15 '20 at 05:46
2 Answers
So, the starter code is as follows:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
The first step would be to create a webdriver
object.
chrome_options = Options()
# Stops the UI interface (chrome browser) from popping up
# chrome_options.add_argument("--headless")
driver = webdriver.Chrome(executable_path='/path/to/chrome_driver', options=chrome_options)
Spin up the browser with the URL
page_url = "https://www.tradingview.com/chart/lUsimB6z/"
driver.get(page_url)
This driver
element contains methods that you can use to fetch elements from the html.
The complete page source can be obtained by using driver.page_source
. You can switch over to BeautifulSoup
if needed, by simply using soup = BeautifulSoup(driver.page_source, "html.parser")
Some of the methods you can use with the driver
object are:
driver.find_element_by_tag_name(tag_name)
driver.find_element(s)_by_class_name(class_name)
driver.save_screenshot()
etc...
This link explains how you can simulate scrolling on the browser using the driver
object to fetch the table data.
Finally,
driver.quit()
You'll need to have chromedriver be located at '/path/to/chrome_driver'
for selenium to work.

- 918
- 9
- 18
Just in case anyone is interested in this if you want to scrape very small timeframes (1s, 5s). The method described above poses an issue because the website updates every 10 seconds. The way I found is described below:
- use chrome or brave
- add the autoview extension
- it will add a new tab in the Strategy tester, named 'Export'
- use selenium to click on that button to save the strategy results to clipboard and then paste them into a dataframe
- export to csv
here's a sample of the code:
#click the Strategy tester tab so that the button is visible
c = driver.find_element_by_xpath('//*[@id="footer-chart-panel"]/div[1]/div[1]/div[4]/div/span')
driver.execute_script("$(arguments[0]).click();", c)
#click the Export button
z = driver.find_element_by_xpath('//li[@class="autoview-backtesting-export"]')
driver.execute_script("$(arguments[0]).click();", z)
If any of this fails, try and add a loop (0-5) and put the action within the loop. The rest is pandas-related.
#copy the clipboard into dataframe
a = pd.read_clipboard(sep='\t', usecols=[])

- 135
- 1
- 9