aliceblue11 commited on
Commit
93858c0
·
verified ·
1 Parent(s): eec2f50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -10
app.py CHANGED
@@ -3,6 +3,9 @@ import requests
3
  from bs4 import BeautifulSoup
4
  import pandas as pd
5
  import gradio as gr
 
 
 
6
 
7
  # 스크래핑 함수
8
  def scrape_naver_stock():
@@ -22,14 +25,14 @@ def scrape_naver_stock():
22
  if len(cols) > 1:
23
  rank = cols[0].text.strip()
24
  name = cols[1].text.strip()
25
- price = cols[2].text.strip()
26
- diff = cols[3].text.strip().replace("상한가", "▲").replace("상승", "▲")
27
- change_rate = cols[4].text.strip()
28
- volume = cols[5].text.strip()
29
- buy_price = cols[6].text.strip()
30
- sell_price = cols[7].text.strip()
31
- buy_volume = cols[8].text.strip()
32
- sell_volume = cols[9].text.strip()
33
  per = cols[10].text.strip()
34
  roe = cols[11].text.strip()
35
  data.append([rank, name, price, diff, change_rate, volume, buy_price, sell_price, buy_volume, sell_volume, per, roe])
@@ -37,9 +40,13 @@ def scrape_naver_stock():
37
  # Pandas DataFrame으로 변환
38
  df = pd.DataFrame(data, columns=['순위', '종목명', '현재가', '전일비', '등락률', '거래량', '매수호가', '매도호가', '매수총잔량', '매도총잔량', 'PER', 'ROE'])
39
 
 
 
 
 
40
  return df
41
 
42
- # 엑셀 파일 저장 함수
43
  def save_to_excel():
44
  df = scrape_naver_stock()
45
  directory = "/mnt/data"
@@ -49,7 +56,26 @@ def save_to_excel():
49
  os.makedirs(directory)
50
 
51
  file_path = os.path.join(directory, "naver_stock_data.xlsx")
52
- df.to_excel(file_path, index=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  return file_path
54
 
55
  # 그라디오 UI
 
3
  from bs4 import BeautifulSoup
4
  import pandas as pd
5
  import gradio as gr
6
+ from openpyxl import load_workbook
7
+ from openpyxl.utils import get_column_letter
8
+ from openpyxl.styles import Alignment
9
 
10
  # 스크래핑 함수
11
  def scrape_naver_stock():
 
25
  if len(cols) > 1:
26
  rank = cols[0].text.strip()
27
  name = cols[1].text.strip()
28
+ price = cols[2].text.strip().replace(",", "")
29
+ diff = cols[3].text.strip().replace("상한가", "▲").replace("상승", "▲").replace(",", "")
30
+ change_rate = cols[4].text.strip().replace("%", "").replace("+", "")
31
+ volume = cols[5].text.strip().replace(",", "")
32
+ buy_price = cols[6].text.strip().replace(",", "")
33
+ sell_price = cols[7].text.strip().replace(",", "")
34
+ buy_volume = cols[8].text.strip().replace(",", "")
35
+ sell_volume = cols[9].text.strip().replace(",", "")
36
  per = cols[10].text.strip()
37
  roe = cols[11].text.strip()
38
  data.append([rank, name, price, diff, change_rate, volume, buy_price, sell_price, buy_volume, sell_volume, per, roe])
 
40
  # Pandas DataFrame으로 변환
41
  df = pd.DataFrame(data, columns=['순위', '종목명', '현재가', '전일비', '등락률', '거래량', '매수호가', '매도호가', '매수총잔량', '매도총잔량', 'PER', 'ROE'])
42
 
43
+ # 숫자 컬럼을 숫자 형식으로 변환
44
+ numeric_columns = ['현재가', '전일비', '등락률', '거래량', '매수호가', '매도호가', '매수총잔량', '매도총잔량']
45
+ df[numeric_columns] = df[numeric_columns].apply(pd.to_numeric, errors='coerce')
46
+
47
  return df
48
 
49
+ # 엑셀 파일 저장 및 스타일 적용 함수
50
  def save_to_excel():
51
  df = scrape_naver_stock()
52
  directory = "/mnt/data"
 
56
  os.makedirs(directory)
57
 
58
  file_path = os.path.join(directory, "naver_stock_data.xlsx")
59
+
60
+ # 엑셀로 저장
61
+ df.to_excel(file_path, index=False, engine='openpyxl')
62
+
63
+ # 엑셀 파일 불러오기
64
+ wb = load_workbook(file_path)
65
+ ws = wb.active
66
+
67
+ # 숫자 데이터가 있는 열을 우측 정렬로 설정
68
+ alignment = Alignment(horizontal='right')
69
+
70
+ # 숫자 형식 적용할 열들 (1번째는 순위이므로 제외)
71
+ for col in range(3, ws.max_column + 1):
72
+ for row in range(2, ws.max_row + 1): # 첫 번째 행은 헤더이므로 제외
73
+ cell = ws[f"{get_column_letter(col)}{row}"]
74
+ cell.alignment = alignment
75
+
76
+ # 엑셀 파일 저장
77
+ wb.save(file_path)
78
+
79
  return file_path
80
 
81
  # 그라디오 UI