I'm trying to use Selenium to pull the stock symbols from this page and insert them into a list: https://www.barchart.com/stocks/highs-lows/highs?timeFrame=1y
My code:
StockList = []
driver = webdriver.Chrome()
url = "https://www.barchart.com/stocks/highs-lows/highs?timeFrame=1y"
driver.get(url)
stock_list = driver.find_elements_by_tag_name("tr")
for stock in stock_list:
stock.find_element_by_name("data-current-symbol=")
print(stock)
I receive a NoSuchElementException
.
When I inspect the page, each tr
has the following: "data-current-symbol="ACY
How can I pull out the stock symbol?
CodePudding user response:
Use this selector:
tr[data-current-symbol]
And utilize .get_attribute
method:
StockList = []
driver = webdriver.Chrome()
url = "https://www.barchart.com/stocks/highs-lows/highs?timeFrame=1y"
driver.get(url)
stock_list = driver.find_elements_by_css_selector("tr[data-current-symbol]")
for stock in stock_list:
symbol = stock.get_attribute('data-current-symbol')
print(symbol)
StockList.append(symbol)