diff --git a/bdshare/stock/market.py b/bdshare/stock/market.py index a9d6244..c8335e7 100644 --- a/bdshare/stock/market.py +++ b/bdshare/stock/market.py @@ -5,59 +5,92 @@ from bdshare.util import vars as vs -def get_market_inf(): +def get_market_inf(retry_count=3, pause=0.001): + """ + get company information. + :return: dataframe + """ + for _ in range(retry_count): + time.sleep(pause) + try: + r = requests.get(vs.DSE_URL+vs.DSE_MARKET_INF_URL) + if r.status_code != 200: + r = requests.get(vs.DSE_ALT_URL+vs.DSE_MARKET_INF_URL) + except Exception as e: + print(e) + else: + soup = BeautifulSoup(r.text, 'html.parser') + quotes = [] # a list to store quotes + + table = soup.find('table', attrs={'class': 'table table-bordered background-white text-center', '_id': 'data-table'}) + + for row in table.find_all('tr')[1:]: + cols = row.find_all('td') + quotes.append({'Date': cols[0].text.strip().replace(",", ""), + 'Total Trade': cols[1].text.strip().replace(",", ""), + 'Total Volume': cols[2].text.strip().replace(",", ""), + 'Total Value (mn)': cols[3].text.strip().replace(",", ""), + 'Total Market Cap. (mn)': cols[4].text.strip().replace(",", ""), + 'DSEX Index': cols[5].text.strip().replace(",", ""), + 'DSES Index': cols[6].text.strip().replace(",", ""), + 'DS30 Index': cols[7].text.strip().replace(",", ""), + 'DGEN Index': cols[8].text.strip().replace(",", "") + }) + df = pd.DataFrame(quotes) + return df + +def get_company_inf(symbol=None, retry_count=3, pause=0.001): """ get stock market information. + :param symbol: str, Instrument symbol e.g.: 'ACI' or 'aci' :return: dataframe """ - r = requests.get(vs.DSE_URL+vs.DSE_MARKET_INF_URL) - soup = BeautifulSoup(r.text, 'html.parser') - quotes = [] # a list to store quotes - - table = soup.find('table', attrs={'class': 'table table-bordered background-white text-center', '_id': 'data-table'}) - - for row in table.find_all('tr')[1:]: - cols = row.find_all('td') - quotes.append({'Date': cols[0].text.strip().replace(",", ""), - 'Total Trade': cols[1].text.strip().replace(",", ""), - 'Total Volume': cols[2].text.strip().replace(",", ""), - 'Total Value (mn)': cols[3].text.strip().replace(",", ""), - 'Total Market Cap. (mn)': cols[4].text.strip().replace(",", ""), - 'DSEX Index': cols[5].text.strip().replace(",", ""), - 'DSES Index': cols[6].text.strip().replace(",", ""), - 'DS30 Index': cols[7].text.strip().replace(",", ""), - 'DGEN Index': cols[8].text.strip().replace(",", "") - }) - df = pd.DataFrame(quotes) - return df - - -def get_latest_pe(): + data = {'name': symbol} + + for _ in range(retry_count): + time.sleep(pause) + try: + r = requests.get(vs.DSE_URL+vs.DSE_COMPANY_INF_URL, params=data) + if r.status_code != 200: + r = requests.get(vs.DSE_ALT_URL+vs.DSE_COMPANY_INF_URL, params=data) + except Exception as e: + print(e) + else: + soup = BeautifulSoup(r.content, 'html5lib') + print(soup) + +def get_latest_pe(retry_count=3, pause=0.001): """ get last stock P/E. :return: dataframe """ - r = requests.get(vs.DSE_URL+vs.DSE_LPE_URL) - # soup = BeautifulSoup(r.text, 'html.parser') - soup = BeautifulSoup(r.content, 'html5lib') - - quotes = [] # a list to store quotes - table = soup.find('table', attrs={'class': 'table table-bordered background-white shares-table fixedHeader'}) - for row in table.find_all('tr')[1:]: - cols = row.find_all('td') - quotes.append((cols[1].text.strip().replace(",", ""), - cols[2].text.strip().replace(",", ""), - cols[3].text.strip().replace(",", ""), - cols[4].text.strip().replace(",", ""), - cols[5].text.strip().replace(",", ""), - cols[6].text.strip().replace(",", ""), - cols[7].text.strip().replace(",", ""), - cols[8].text.strip().replace(",", ""), - cols[9].text.strip().replace(",", "") - )) - df = pd.DataFrame(quotes) - return df + for _ in range(retry_count): + time.sleep(pause) + try: + r = requests.get(vs.DSE_URL+vs.DSE_LPE_URL) + if r.status_code != 200: + r = requests.get(vs.DSE_ALT_URL+vs.DSE_LPE_URL) + except Exception as e: + print(e) + else: + soup = BeautifulSoup(r.content, 'html5lib') + quotes = [] # a list to store quotes + table = soup.find('table', attrs={'class': 'table table-bordered background-white shares-table fixedHeader'}) + for row in table.find_all('tr')[1:]: + cols = row.find_all('td') + quotes.append((cols[1].text.strip().replace(",", ""), + cols[2].text.strip().replace(",", ""), + cols[3].text.strip().replace(",", ""), + cols[4].text.strip().replace(",", ""), + cols[5].text.strip().replace(",", ""), + cols[6].text.strip().replace(",", ""), + cols[7].text.strip().replace(",", ""), + cols[8].text.strip().replace(",", ""), + cols[9].text.strip().replace(",", "") + )) + df = pd.DataFrame(quotes) + return df def get_market_inf_more_data(start=None, end=None, index=None, retry_count=3, pause=0.001): """ diff --git a/bdshare/util/vars.py b/bdshare/util/vars.py index 30da5bf..c157351 100644 --- a/bdshare/util/vars.py +++ b/bdshare/util/vars.py @@ -10,6 +10,7 @@ DSE_NEWS_URL = "old_news.php" DSE_CLOSE_PRICE_URL = "dse_close_price_archive.php" DSE_COMPANY_LIST_URL = "company_listing.php" +DSE_COMPANY_INF_URL = "displayCompany.php" DSE_MARKET_INF_URL = "recent_market_information.php" DSE_MARKET_INF_MORE_URL = "recent_market_information_more.php"