Skip to content

Commit

Permalink
Fix more exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmysitu committed Nov 26, 2023
1 parent 00b0f80 commit b96687a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
25 changes: 25 additions & 0 deletions examples/get_xshg.py
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 25 additions & 0 deletions examples/get_xtai.py
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 14 additions & 9 deletions msfinance/stocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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']")
Expand All @@ -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(
Expand Down

0 comments on commit b96687a

Please sign in to comment.