def search_stock(): try: search = request.form['input'] search_word = web_engine().search('{}'.format(search)) # 검색 엔진 search_word_code = str(search_word[0]).zfill(6) institution_foriegner = foreinger_institurion( ).show_seach_page_foreigner_instition(search_word_code) news_data = naver_news().news_information(search_word_code) financial_data = korea_detail_information().kr_financial_data( search_word_code) main_page_value = main_page_information().main_page_data() stock_information = detail_serach_page_imformation().main_information( search_word_code) nonprice_data = korea_detail_information().kr_detail_data( search_word_code) price_data = korea_detail_information().kr_price_data( int(search_word_code)) price_diff_data = kr_page_data().change_rate_calculate()[int( search_word_code)] return render_template('kr_search_detail.html', search_word_code=search_word_code, main_page_value=main_page_value, news_data=news_data, nonprice_data=nonprice_data, price_data=price_data, financial_data=financial_data, institution_foriegner=institution_foriegner, stock_information=stock_information, price_diff=price_diff_data, current_time=date_method()) except: return render_template('error_search_detail.html')
def daily_kospi_data_update(self): engine = create_engine("mysql+pymysql://root:" + "0000" + "@127.0.0.1/web_db?charset=utf8", encoding='utf-8') conn = engine.connect() today_data = wb.DataReader("^KS11","yahoo",datetime.date.today()) today_data.columns = ['high','low','open','close','volume','adj_close'] today_data['type'] = 'kospi' today_data['date'] = date_method().date_num() today_data.to_sql(name='kr_index_data_daily', con=engine, if_exists='append', index=False) conn.close()
def korea_stock(): main_page_value = main_page_information().main_page_data() foreinger_institurion_data = foreinger_institurion( ).daily_data_db_to_python() kospi_change_data = kr_page_data().volatilty('kospi') kosdaq_change_data = kr_page_data().volatilty('kosdaq') kospi_volume_data = kr_page_data().volume('kospi') kosdaq_volume_data = kr_page_data().volume('kosdaq') return render_template( 'korea_stock.html', main_page_value=main_page_value, current_time=date_method(), foreinger_institurion_data=foreinger_institurion_data, kospi_change_data=kospi_change_data, kosdaq_change_data=kosdaq_change_data, kospi_volume_data=kospi_volume_data, kosdaq_volume_data=kosdaq_volume_data)
def main_page(): main_page_value = main_page_information().main_page_data() kospi_kosdaq_sum = main_page_information().main_page_calculate() kospi_change_data = kr_page_data().volatilty('kospi') kosdaq_change_data = kr_page_data().volatilty('kosdaq') kospi_low_value_data = low_value_stock().final_data_to_df('kospi')[1] kosdaq_low_value_data = low_value_stock().final_data_to_df('kosdaq')[1] return render_template('main_page.html', main_page_value=main_page_value, kospi_kosdaq_sum=kospi_kosdaq_sum, kospi_change_data=kospi_change_data, kosdaq_change_data=kosdaq_change_data, current_time=date_method().time(), kospi_low_value_data=kospi_low_value_data, kosdaq_low_value_data=kosdaq_low_value_data)
def final_value(self): main_value = self.main_page_value() world_value = self.world_page_value() final_list = [ main_value[0], main_value[1], main_value[2], main_value[6], main_value[7], main_value[8], world_value[3], world_value[4], world_value[5], world_value[0], world_value[1], world_value[2], world_value[6], world_value[7], world_value[8], world_value[9], world_value[10], world_value[11], main_value[15], main_value[16], main_value[17], main_value[18], main_value[19], main_value[20], main_value[21], main_value[22], main_value[23], main_value[27], main_value[28], main_value[29], main_value[30], main_value[31], main_value[23], main_value[24], main_value[25], main_value[26], main_value[3], main_value[4], main_value[5], main_value[9], main_value[10], main_value[11] ] today_date = date_method().date_num() data = [today_date] + final_list return data
def code_list_update(self): # DB에서 실제 정보가 있는 데이터가 없을때 새롭게 DB에 추가해주기 code_list = self.code_list() total_code_list = code_information().code_data()['kr_stock_code'] non_list_code = list(set([int(i) for i in total_code_list]) - set(code_list)) # DB 엔진 engine = create_engine("mysql+pymysql://root:" + "0000" + "@127.0.0.1/web_db?charset=utf8", encoding='utf-8') conn = engine.connect() if len(non_list_code) != 0: # 우선 없는 데이터 추가 df_krx = fdr.StockListing('KRX') kospi_kosdaq = df_krx[(df_krx['Market'] == 'KOSDAQ') | (df_krx['Market'] == 'KOSPI')].dropna() kospi_kosdaq = kospi_kosdaq[ ['Symbol', 'Market', 'Name', 'Sector', 'Industry', 'ListingDate', 'SettleMonth', 'HomePage', 'Region']] kospi_kosdaq.columns = ['kr_stock_code', 'market', 'kr_stock_name', 'sector', 'industry', 'listingdate', 'settledate', 'homepage', 'region'] kospi_kosdaq['kr_stock_code'] = [int(i) for i in kospi_kosdaq['kr_stock_code']] kospi_kosdaq = kospi_kosdaq.set_index('kr_stock_code') add_df = kospi_kosdaq.loc[non_list_code] add_df = add_df.reset_index() add_df.columns = ['kr_stock_code', 'market', 'kr_stock_name', 'sector', 'industry', 'listingdate', 'settledate', 'homepage', 'region'] # DB kr_stock_list 추가 add_df.to_sql(name='kr_stock_list', con=engine, if_exists='append', index=False) # DB kr_stock_daily 추가 for i in non_list_code: code = str(i).zfill(6) df = fdr.DataReader(code, date_method().date_num()) df = df.reset_index() df['code'] = code df.columns = ['date','kr_stock_open','kr_stock_high','kr_stock_low','kr_stock_close', 'kr_stock_volume','kr_stock_close_diff','kr_stock_code'] df = df[['kr_stock_code','date','kr_stock_open','kr_stock_close_diff','kr_stock_high','kr_stock_low', 'kr_stock_close','kr_stock_volume']] df.to_sql(name='kr_stock_daily', con=engine, if_exists='append', index=False) # 존재하는 데이터 업데이트 for i in code_list: code = str(i).zfill(6) df = fdr.DataReader(code, '2020-09-14') df = df.reset_index() df['code'] = code df.columns = ['date', 'kr_stock_open', 'kr_stock_high', 'kr_stock_low', 'kr_stock_close', 'kr_stock_volume', 'kr_stock_close_diff', 'kr_stock_code'] df = df[ ['kr_stock_code', 'date', 'kr_stock_open', 'kr_stock_close_diff', 'kr_stock_high', 'kr_stock_low', 'kr_stock_close', 'kr_stock_volume']] df.to_sql(name='kr_stock_daily', con=engine, if_exists='append', index=False) conn.close() else: #차이가 없을 경우 존재하는 데이터만 업데이트 for i in code_list: code = str(i).zfill(6) df = fdr.DataReader(code, date_method().date_num()) df = df.reset_index() df['code'] = code df.columns = ['date', 'kr_stock_open', 'kr_stock_high', 'kr_stock_low', 'kr_stock_close', 'kr_stock_volume', 'kr_stock_close_diff', 'kr_stock_code'] df = df[ ['kr_stock_code', 'date', 'kr_stock_open', 'kr_stock_close_diff', 'kr_stock_high', 'kr_stock_low', 'kr_stock_close', 'kr_stock_volume']] df.to_sql(name='kr_stock_daily', con=engine, if_exists='append', index=False) conn.close()
def weekly_data(self): code_list = [ str(i[0]).zfill(6) for i in kr_stock_weekly().kr_stock_code_list() ] final_set = [] for num, code in enumerate(code_list): if num in list(range(100, 10000, 100)): print(round(num / len(code_list), 2) * 100, '% 완료') # 시가총액, 코스피 랭킹 try: URL = "https://finance.naver.com/item/main.nhn?code={}" index = requests.get(URL.format(code)) html = index.text soup = BeautifulSoup(html, 'html.parser') price_info = list( filter(('').__ne__, soup.find('div', class_='rate_info').text.replace( '\t', '').split('\n'))) info1 = soup.find('div', class_='aside_invest_info').find( 'div', class_='first').text.replace('\t', '').replace('\n', '') info2 = soup.find('div', class_='aside_invest_info').find( 'table', class_='rwidth').text.split('\n') info3 = soup.find('div', class_='aside_invest_info').find( 'table', class_='per_table') info4 = soup.find('div', class_='aside_invest_info').find( 'table', summary="동일업종 PER 정보") # OHLCV 정보 today_close = price_info[1][5:-3].strip() today_close_diff = [ price_info[2][-2:], price_info[2][:price_info[2].find('포인트')].strip() ] today_close_percent = [ price_info[3][price_info[3].find('%') + 1:].strip(), price_info[3][:price_info[3].find('%')] ] today_open = price_info[28][:int(len(price_info[28]) / 2)] today_high = price_info[20][:int(len(price_info[20]) / 2)] today_low = price_info[30][:int(len(price_info[30]) / 2)] today_vol = price_info[25] # 시가총액, 코스피 랭킹 (info1) market_list = ['시가총액시가총액', '시가총액순위', '상장주식수', '액면가l매매단위'] market_list_position = [] length = [] for i in range(len(market_list)): length.append(len(market_list[i])) market_list_position.append(info1.find(market_list[i])) position_start = [ x + y for x, y in zip(market_list_position, length) ] position_finish = market_list_position + [len(info1)] basic_info = [] for x, y in zip(position_start, position_finish[1:]): basic_info.append(info1[x:y]) market_value = basic_info[0] kospi_rank = basic_info[1] # 투자의견 (info2) comment = info2[4] goal_price = info2[6] high_52week = info2[-6] low_52week = info2[-4] # PER, EPS, 추정PER,EPS, PER = info3.find_all('em')[0].text EPS = info3.find_all('em')[1].text est_PER = info3.find_all('em')[2].text est_EPS = info3.find_all('em')[3].text PBR = info3.find_all('em')[4].text BPS = info3.find_all('em')[5].text DIV = info3.find_all('em')[6].text # 동일 업종 PER, 예상 등락률 same_ind_per = info4.text.replace('\t', '').split('\n')[5] same_ind_per_percentage = info4.text.replace( '\t', '').split('\n')[-4] # close_diff 값 차이 수정 i = today_close_diff try: if i[0] == '하락': close_diff = -1 * float(i[1].replace(",", '')) today_close_diff = close_diff elif i[0] == '상승': close_diff = float(i[1].replace(",", '')) today_close_diff = close_diff else: close_diff = float(i[1].replace(",", '')) today_close_diff = close_diff except: today_close_diff = close_diff # close_change 값 수정 j = today_close_percent try: if j[0] == '마이너스': close_change = -1 * float(j[1].replace(",", '')) today_close_percent = close_change elif i[0] == '플러스': close_change = float(j[1].replace(",", '')) today_close_percent = close_change else: today_close_percent = j[1] except: today_close_percent = close_change date = [date_method().date_num()] * len(code) final_value = [ code, date, today_close, today_close_diff, today_close_percent, today_open, today_high, today_low, today_vol, market_value, kospi_rank, comment, goal_price, high_52week, low_52week, PER, EPS, est_PER, est_EPS, PBR, BPS, DIV, same_ind_per, same_ind_per_percentage ] except: final_value = [code, '2020-09-04', 'NAN'] final_set.append(final_value) data = pd.DataFrame(columns=[ 'code', 'date', 'close', 'close_diff', 'close_percent', 'open', 'high', 'low', 'vol', 'market_value', 'kospi_rank', 'comment', 'goal_price', 'high_52week', 'low_52week', 'PER', 'EPS', 'est_PER', 'est_EPS', 'PBR', 'BPS', 'DIV', 'same_ind_per', 'same_ind_per_percentage' ]) for i in range(len(final_set)): if len(final_set[i]) == 24: data.loc[i] = final_set[i] else: data.loc[i] = final_set[i] + [ 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN', 'NAN' ] data.columns = [ 'kr_stock_code', 'date', 'kr_stock_close', 'kr_stock_close_diff', 'kr_stock_close_percent', 'kr_stock_open', 'kr_stock_high', 'kr_stock_low', 'kr_stock_vol', 'kr_stock_market_value', 'kr_stock_kospi_rank', 'kr_stock_comment', 'kr_stock_goal_price', 'kr_stock_high_52week', 'kr_stock_low_52week', 'kr_stock_PER', 'kr_stock_EPS', 'kr_stock_est_PER', 'kr_stock_est_EPS', 'kr_stock_PBR', 'kr_stock_BPS', 'kr_stock_DIV', 'kr_stock_same_ind_per', 'kr_stock_same_ind_per_percentage' ] return data
def daily_foreinger_institurion_crolling(self): URL = "https://finance.naver.com/sise/" index1 = requests.get(URL) html = index1.text soup = BeautifulSoup(html, 'html.parser') table = soup.find('div', id='contentarea_left') total_df = pd.DataFrame() foreigner_buy = [] for i in table.find_all('table')[2].text.replace('\t', '').split('\n'): if len(i) > 0: if i == '0': foreigner_buy.append('-') foreigner_buy.append(i) else: foreigner_buy.append(i) foreigner_buy = foreigner_buy[1:] foreigner_buy_df = pd.DataFrame(np.array(foreigner_buy).reshape(-1, 4)) total_df = pd.concat([total_df, foreigner_buy_df], axis=0) foreigner_sell = [] for i in table.find_all('table')[3].text.replace('\t', '').split('\n'): if len(i) > 0: if i == '0': foreigner_sell.append('-') foreigner_sell.append(i) else: foreigner_sell.append(i) foreigner_sell = foreigner_sell[1:] foreigner_sell_df = pd.DataFrame( np.array(foreigner_sell).reshape(-1, 4)) total_df = pd.concat([total_df, foreigner_sell_df], axis=0) institution_buy = [] for i in table.find_all('table')[4].text.replace('\t', '').split('\n'): if len(i) > 0: if i == '0': institution_buy.append('-') institution_buy.append(i) else: institution_buy.append(i) institution_buy = institution_buy[1:] institution_buy_df = pd.DataFrame( np.array(institution_buy).reshape(-1, 4)) total_df = pd.concat([total_df, institution_buy_df], axis=0) institurion_sell = [] for i in table.find_all('table')[5].text.replace('\t', '').split('\n'): if len(i) > 0: if i == '0': institurion_sell.append('-') institurion_sell.append(i) else: institurion_sell.append(i) institurion_sell = institurion_sell[1:] institurion_sell_df = pd.DataFrame( np.array(institurion_sell).reshape(-1, 4)) total_df = pd.concat([total_df, institurion_sell_df], axis=0) total_df.columns = ['kr_stock_name', 'close', '상승or하락', '변동율'] total_df = total_df.reset_index() total_df = total_df[['kr_stock_name', 'close', '상승or하락', '변동율']] total_df['change'] = None for i in range(len(total_df)): if total_df.loc[i]['상승or하락'] == '상승': total_df.loc[i]['change'] = int(total_df.loc[i]['변동율'].replace( ',', '')) elif total_df.loc[i]['상승or하락'] == '하락': total_df.loc[i]['change'] = -1 * int( total_df.loc[i]['변동율'].replace(',', '')) else: total_df.loc[i]['change'] = 0 total_df['date'] = date_method().date_num() total_df['close'] = [ int(i.replace(",", "")) for i in total_df['close'] ] total_df = total_df[['kr_stock_name', 'date', 'close', 'change']] return total_df