From b96687a4342058d93d8df64f31110ddb985492a7 Mon Sep 17 00:00:00 2001 From: Jimmy Situ Date: Sun, 26 Nov 2023 14:03:55 +0800 Subject: [PATCH] Fix more exception --- examples/get_xshg.py | 25 +++++++++++++++++++++++++ examples/get_xtai.py | 25 +++++++++++++++++++++++++ msfinance/stocks.py | 23 ++++++++++++++--------- 3 files changed, 64 insertions(+), 9 deletions(-) create mode 100755 examples/get_xshg.py create mode 100755 examples/get_xtai.py diff --git a/examples/get_xshg.py b/examples/get_xshg.py new file mode 100755 index 0000000..f216140 --- /dev/null +++ b/examples/get_xshg.py @@ -0,0 +1,25 @@ +#!/usr/bin/python3 -u + +import msfinance as msf + +proxy = 'socks5://127.0.0.1:1088' + +stock = msf.Stock( + debug=True, + session='xshg.sql3', + proxy=proxy, +) + +tickers_list = { + '603288', +} + +for ticker in sorted(tickers_list): + valuations = stock.get_valuations(ticker, 'xshg') + financials = stock.get_financials(ticker, 'xshg') + + print(f"Ticker: {ticker}") + for valuation in valuations: + print(valuation) + for financial in financials: + print(financial) diff --git a/examples/get_xtai.py b/examples/get_xtai.py new file mode 100755 index 0000000..a33b4bd --- /dev/null +++ b/examples/get_xtai.py @@ -0,0 +1,25 @@ +#!/usr/bin/python3 -u + +import msfinance as msf + +proxy = 'socks5://127.0.0.1:1088' + +stock = msf.Stock( + debug=True, + session='xtai.sql3', + proxy=proxy, +) + +tickers_list = { + '2454', '2330', +} + +for ticker in sorted(tickers_list): + valuations = stock.get_valuations(ticker, 'xtai') + financials = stock.get_financials(ticker, 'xtai') + + print(f"Ticker: {ticker}") + for valuation in valuations: + print(valuation) + for financial in financials: + print(financial) diff --git a/msfinance/stocks.py b/msfinance/stocks.py index 5a217bb..9abacf4 100644 --- a/msfinance/stocks.py +++ b/msfinance/stocks.py @@ -20,6 +20,7 @@ from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import ElementClickInterceptedException +from selenium.common.exceptions import ElementNotInteractableException from selenium.common.exceptions import TimeoutException @@ -228,9 +229,9 @@ def _get_financials(self, ticker, exchange, statement, period='Annual', stage='R url = f"https://www.morningstar.com/stocks/{exchange}/{ticker}/financials" self.driver.get(url) - # Select income statement - income_button = self.driver.find_element(By.XPATH, f"//button[contains(., '{statement}')]") - income_button.click() + # Select statement type + type_button = self.driver.find_element(By.XPATH, f"//button[contains(., '{statement}')]") + type_button.click() # Select statement period period_list_button = self.driver.find_element(By.XPATH, "//button[contains(., 'Annual') and @aria-haspopup='true']") @@ -251,24 +252,28 @@ def _get_financials(self, ticker, exchange, statement, period='Annual', stage='R except ElementClickInterceptedException: pass - # Select statement type - type_list_button = self.driver.find_element(By.XPATH, "//button[contains(., 'As Originally Reported') and @aria-haspopup='true']") + # Select statement stage + stage_list_button = self.driver.find_element(By.XPATH, "//button[contains(., 'As Originally Reported') and @aria-haspopup='true']") try: - type_list_button.click() + stage_list_button.click() time.sleep(1) except ElementClickInterceptedException: pass + except ElementNotInteractableException: + pass if 'As Originally Reported' == stage: - type_button = self.driver.find_element(By.XPATH, "//span[contains(., 'As Originally Reported') and @class='mds-list-group__item-text__sal']") + stage_button = self.driver.find_element(By.XPATH, "//span[contains(., 'As Originally Reported') and @class='mds-list-group__item-text__sal']") else: - type_button = self.driver.find_element(By.XPATH, "//span[contains(., 'Restated') and @class='mds-list-group__item-text__sal']") + stage_button = self.driver.find_element(By.XPATH, "//span[contains(., 'Restated') and @class='mds-list-group__item-text__sal']") try: - type_button.click() + stage_button.click() time.sleep(1) except ElementClickInterceptedException: pass + except ElementNotInteractableException: + pass # Expand the detail page expand_detail_view = WebDriverWait(self.driver, 30).until(