unnastyle commited on
Commit
2afd120
ยท
verified ยท
1 Parent(s): 4965870

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -20
app.py CHANGED
@@ -10,11 +10,9 @@ def debug_print(*args):
10
  def scrape_data(market_type: str):
11
  """
12
  market_type (str): '0' -> ์ฝ”์Šคํ”ผ, '1' -> ์ฝ”์Šค๋‹ฅ
13
- ๋„ค์ด๋ฒ„ ์ฆ๊ถŒ์˜ '์ƒ์Šน ์ข…๋ชฉ' ํŽ˜์ด์ง€์—์„œ ํ•ด๋‹น market_type์˜ ์ •๋ณด๋ฅผ ์Šคํฌ๋ž˜ํ•‘.
14
- (BeautifulSoup / lxml ์—†์ด ์ •๊ทœํ‘œํ˜„์‹๋งŒ ์‚ฌ์šฉ)
15
  """
16
- # market_type์— ๋”ฐ๋ผ URL ์„ค์ •
17
- # '0'์ด๋ฉด ์ฝ”์Šคํ”ผ, '1'์ด๋ฉด ์ฝ”์Šค๋‹ฅ
18
  base_url = "https://finance.naver.com/sise/sise_rise.naver?sosok="
19
  url = base_url + market_type
20
  debug_print("Requesting URL:", url)
@@ -56,7 +54,6 @@ def scrape_data(market_type: str):
56
 
57
  # HTML ํƒœ๊ทธ ์ œ๊ฑฐ ํ—ฌํผ ํ•จ์ˆ˜
58
  def clean_html(raw_html):
59
- # ๋ชจ๋“  ํƒœ๊ทธ ์ œ๊ฑฐ
60
  text = re.sub(r'<.*?>', '', raw_html, flags=re.DOTALL)
61
  return text.strip()
62
 
@@ -96,43 +93,61 @@ def scrape_data(market_type: str):
96
  def make_table(market_choice):
97
  """
98
  market_choice (str): "์ฝ”์Šคํ”ผ" ๋˜๋Š” "์ฝ”์Šค๋‹ฅ"
99
- ํ•ด๋‹น ์„ ํƒ์— ๋”ฐ๋ผ scrape_data()๋ฅผ ์‹คํ–‰ํ•œ ๋’ค DataFrame์œผ๋กœ ๋ฐ˜ํ™˜.
 
 
100
  """
101
  debug_print(f"Scraping data for market_choice={market_choice}...")
102
- # ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒ๋ฐ•์Šค์—์„œ "์ฝ”์Šคํ”ผ" ์„ ํƒ -> '0'
103
- # ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒ๋ฐ•์Šค์—์„œ "์ฝ”์Šค๋‹ฅ" ์„ ํƒ -> '1'
104
  market_type = "0" if market_choice == "์ฝ”์Šคํ”ผ" else "1"
105
- data = scrape_data(market_type)
106
 
 
107
  if not data:
108
  debug_print("No data retrieved or table not found.")
109
- return pd.DataFrame(["๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค."])
 
110
 
111
  debug_print("Scraping done. Converting to DataFrame.")
112
- return pd.DataFrame(data)
 
 
 
 
 
 
 
 
 
 
 
113
 
114
  def main():
115
  """
116
  Gradio ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
117
  """
118
  with gr.Blocks() as demo:
119
- gr.Markdown("# ๋„ค์ด๋ฒ„ ์ฆ๊ถŒ ์Šคํฌ๋ž˜ํ•‘ : ์ฝ”์Šคํ”ผ / ์ฝ”์Šค๋‹ฅ ์„ ํƒ")
 
120
 
121
- # ์„ ํƒ๋ฐ•์Šค: ์ฝ”์Šคํ”ผ / ์ฝ”์Šค๋‹ฅ
122
- market_choice = gr.Dropdown(
123
  label="์ข…๋ชฉ ์„ ํƒ",
124
  choices=["์ฝ”์Šคํ”ผ", "์ฝ”์Šค๋‹ฅ"],
125
  value="์ฝ”์Šคํ”ผ" # ๊ธฐ๋ณธ๊ฐ’
126
  )
127
 
128
- # ๋ฒ„ํŠผ๊ณผ ๊ฒฐ๊ณผ์šฉ DataFrame
129
  scrape_btn = gr.Button("๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ")
130
- output_df = gr.DataFrame(label="์Šคํฌ๋ž˜ํ•‘ ๊ฒฐ๊ณผ")
131
 
132
- # ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์Šคํฌ๋ž˜ํ•‘ ํ•จ์ˆ˜ ํ˜ธ์ถœ
133
- scrape_btn.click(fn=make_table,
134
- inputs=market_choice,
135
- outputs=output_df)
 
 
 
 
 
136
 
137
  demo.launch()
138
 
 
10
  def scrape_data(market_type: str):
11
  """
12
  market_type (str): '0' -> ์ฝ”์Šคํ”ผ, '1' -> ์ฝ”์Šค๋‹ฅ
13
+ ๋„ค์ด๋ฒ„ ์ฆ๊ถŒ์˜ '์ƒ์Šน ์ข…๋ชฉ' ํŽ˜์ด์ง€์—์„œ ํ•ด๋‹น market_type ์ •๋ณด๋ฅผ ์Šคํฌ๋ž˜ํ•‘.
14
+ (BeautifulSoup / lxml ์—†์ด re ๋ชจ๋“ˆ๋งŒ ์‚ฌ์šฉ)
15
  """
 
 
16
  base_url = "https://finance.naver.com/sise/sise_rise.naver?sosok="
17
  url = base_url + market_type
18
  debug_print("Requesting URL:", url)
 
54
 
55
  # HTML ํƒœ๊ทธ ์ œ๊ฑฐ ํ—ฌํผ ํ•จ์ˆ˜
56
  def clean_html(raw_html):
 
57
  text = re.sub(r'<.*?>', '', raw_html, flags=re.DOTALL)
58
  return text.strip()
59
 
 
93
  def make_table(market_choice):
94
  """
95
  market_choice (str): "์ฝ”์Šคํ”ผ" ๋˜๋Š” "์ฝ”์Šค๋‹ฅ"
96
+ - Radio๋ฐ•์Šค์—์„œ ์„ ํƒํ•œ ๋ฌธ์ž์—ด(์ฝ”์Šคํ”ผ -> '0', ์ฝ”์Šค๋‹ฅ -> '1')
97
+ - ํ•ด๋‹น ํŽ˜์ด์ง€ ์Šคํฌ๋ž˜ํ•‘ ํ›„, pandas DataFrame์— ๋‹ด๊ณ 
98
+ '์ „์ผ๋น„', '๋“ฑ๋ฝ๋ฅ ' ์ปฌ๋Ÿผ๋งŒ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ์Šคํƒ€์ผ ์ ์šฉํ•˜์—ฌ HTML ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜
99
  """
100
  debug_print(f"Scraping data for market_choice={market_choice}...")
101
+ # ์„ ํƒ์— ๋”ฐ๋ฅธ sosok ๊ฐ’
 
102
  market_type = "0" if market_choice == "์ฝ”์Šคํ”ผ" else "1"
 
103
 
104
+ data = scrape_data(market_type)
105
  if not data:
106
  debug_print("No data retrieved or table not found.")
107
+ # ๋‹จ์ˆœ ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€๋งŒ ๋ฐ˜ํ™˜
108
+ return "<p>๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.</p>"
109
 
110
  debug_print("Scraping done. Converting to DataFrame.")
111
+ df = pd.DataFrame(data)
112
+
113
+ # '์ „์ผ๋น„', '๋“ฑ๋ฝ๋ฅ ' ์ปฌ๋Ÿผ๋งŒ ๋นจ๊ฐ„์ƒ‰ ์ ์šฉ
114
+ # (pandas ์Šคํƒ€์ผ๋ง -> HTML ๋ณ€ํ™˜)
115
+ df_styled = (
116
+ df.style
117
+ .applymap(lambda _: "color: red", subset=["์ „์ผ๋น„", "๋“ฑ๋ฝ๋ฅ "]) # ํ•ด๋‹น ์ปฌ๋Ÿผ ๋นจ๊ฐ„์ƒ‰
118
+ .hide_index() # ์ธ๋ฑ์Šค ์ œ๊ฑฐ(์„ ํƒ)
119
+ )
120
+ html_table = df_styled.render()
121
+
122
+ return html_table
123
 
124
  def main():
125
  """
126
  Gradio ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
127
  """
128
  with gr.Blocks() as demo:
129
+ gr.Markdown("# ๋„ค์ด๋ฒ„ ์ฆ๊ถŒ ์Šคํฌ๋ž˜ํ•‘ : ์ฝ”์Šคํ”ผ / ์ฝ”์Šค๋‹ฅ ์„ ํƒ\n"
130
+ "- ์ „์ผ๋น„, ๋“ฑ๋ฝ๋ฅ ์€ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.")
131
 
132
+ # Radio๋ฐ•์Šค: ์ฝ”์Šคํ”ผ / ์ฝ”์Šค๋‹ฅ
133
+ market_choice = gr.Radio(
134
  label="์ข…๋ชฉ ์„ ํƒ",
135
  choices=["์ฝ”์Šคํ”ผ", "์ฝ”์Šค๋‹ฅ"],
136
  value="์ฝ”์Šคํ”ผ" # ๊ธฐ๋ณธ๊ฐ’
137
  )
138
 
139
+ # ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ๋ฒ„ํŠผ
140
  scrape_btn = gr.Button("๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ")
 
141
 
142
+ # ๊ฒฐ๊ณผ ์ถœ๋ ฅ์šฉ: HTML ์ปดํฌ๋„ŒํŠธ
143
+ output_html = gr.HTML(label="์Šคํฌ๋ž˜ํ•‘ ๊ฒฐ๊ณผ")
144
+
145
+ # ๋ฒ„ํŠผ ํด๋ฆญ -> make_table() ํ˜ธ์ถœ
146
+ scrape_btn.click(
147
+ fn=make_table,
148
+ inputs=market_choice,
149
+ outputs=output_html
150
+ )
151
 
152
  demo.launch()
153