Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,17 +18,35 @@ def scrape_naver_stock_rise():
|
|
| 18 |
for row in rows[2:]: # μμ 2κ°μ rowλ ν€λ λ° λΉ μΉΈμ΄λ―λ‘ μ μΈ
|
| 19 |
columns = row.find_all('td')
|
| 20 |
if len(columns) > 1:
|
| 21 |
-
rank = columns[0].text.strip()
|
| 22 |
name = columns[1].text.strip()
|
| 23 |
-
current_price = columns[2].text.strip()
|
| 24 |
-
price_diff = columns[3].text.strip().replace('μνκ°', '').replace('μμΉ', '').strip()
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# DataFrameμΌλ‘ λ³ν
|
| 31 |
-
df = pd.DataFrame(stock_data, columns=['μμ', 'μ’
λͺ©λͺ
', 'νμ¬κ°', 'μ μΌλΉ', 'λ±λ½λ₯ ', 'κ±°λλ'])
|
| 32 |
return df
|
| 33 |
|
| 34 |
# μμ
λ‘ μ μ₯νλ ν¨μ
|
|
@@ -42,12 +60,12 @@ def save_to_excel():
|
|
| 42 |
def display_and_save_stocks():
|
| 43 |
stock_data = scrape_naver_stock_rise()
|
| 44 |
file_path = save_to_excel()
|
| 45 |
-
return stock_data, file_path
|
| 46 |
|
| 47 |
# Gradio UI μ€μ
|
| 48 |
iface = gr.Interface(fn=display_and_save_stocks,
|
| 49 |
inputs=[],
|
| 50 |
-
outputs=[gr.
|
| 51 |
title="λ€μ΄λ² μ¦κΆ μμΉ μ’
λͺ© μ€ν¬λν",
|
| 52 |
description="λ€μ΄λ² μ¦κΆμμ νμ¬ μμΉ μ€μΈ μμ μ’
λͺ©μ μ€ν¬λννμ¬ νλ‘ λ³΄μ¬μ£Όκ³ , μμ
νμΌλ‘ μ μ₯ν©λλ€.")
|
| 53 |
iface.launch()
|
|
|
|
| 18 |
for row in rows[2:]: # μμ 2κ°μ rowλ ν€λ λ° λΉ μΉΈμ΄λ―λ‘ μ μΈ
|
| 19 |
columns = row.find_all('td')
|
| 20 |
if len(columns) > 1:
|
| 21 |
+
rank = int(columns[0].text.strip())
|
| 22 |
name = columns[1].text.strip()
|
| 23 |
+
current_price = int(columns[2].text.replace(",", "").strip())
|
| 24 |
+
price_diff = columns[3].text.strip().replace(",", "").replace('μνκ°', '').replace('μμΉ', '').strip()
|
| 25 |
+
price_diff = int(price_diff) if price_diff else 0
|
| 26 |
+
change_rate = columns[4].text.strip().replace("%", "").replace(",", "").strip()
|
| 27 |
+
change_rate = float(change_rate) if change_rate else 0.0
|
| 28 |
+
volume = int(columns[5].text.replace(",", "").strip())
|
| 29 |
+
bid_price = int(columns[6].text.replace(",", "").strip())
|
| 30 |
+
ask_price = int(columns[7].text.replace(",", "").strip())
|
| 31 |
+
bid_total_volume = int(columns[8].text.replace(",", "").strip())
|
| 32 |
+
ask_total_volume = int(columns[9].text.replace(",", "").strip())
|
| 33 |
+
per = float(columns[10].text.strip()) if columns[10].text.strip() else None
|
| 34 |
+
roe = float(columns[11].text.strip()) if columns[11].text.strip() else None
|
| 35 |
|
| 36 |
+
# κ°κ²© λ³λμ λ°λ₯Έ μμ νμ
|
| 37 |
+
if price_diff > 0:
|
| 38 |
+
price_diff = f"<span style='color:red;'>{price_diff}</span>"
|
| 39 |
+
change_rate = f"<span style='color:red;'>{change_rate}%</span>"
|
| 40 |
+
elif price_diff < 0:
|
| 41 |
+
price_diff = f"<span style='color:blue;'>{price_diff}</span>"
|
| 42 |
+
change_rate = f"<span style='color:blue;'>{change_rate}%</span>"
|
| 43 |
+
else:
|
| 44 |
+
change_rate = f"{change_rate}%"
|
| 45 |
+
|
| 46 |
+
stock_data.append([rank, name, current_price, price_diff, change_rate, volume, bid_price, ask_price, bid_total_volume, ask_total_volume, per, roe])
|
| 47 |
|
| 48 |
# DataFrameμΌλ‘ λ³ν
|
| 49 |
+
df = pd.DataFrame(stock_data, columns=['μμ', 'μ’
λͺ©λͺ
', 'νμ¬κ°', 'μ μΌλΉ', 'λ±λ½λ₯ ', 'κ±°λλ', 'λ§€μνΈκ°', 'λ§€λνΈκ°', 'λ§€μμ΄μλ', 'λ§€λμ΄μλ', 'PER', 'ROE'])
|
| 50 |
return df
|
| 51 |
|
| 52 |
# μμ
λ‘ μ μ₯νλ ν¨μ
|
|
|
|
| 60 |
def display_and_save_stocks():
|
| 61 |
stock_data = scrape_naver_stock_rise()
|
| 62 |
file_path = save_to_excel()
|
| 63 |
+
return stock_data.to_html(escape=False), file_path # escape=Falseλ‘ HTML νκ·Έκ° μ μ©λλλ‘ ν¨
|
| 64 |
|
| 65 |
# Gradio UI μ€μ
|
| 66 |
iface = gr.Interface(fn=display_and_save_stocks,
|
| 67 |
inputs=[],
|
| 68 |
+
outputs=[gr.HTML(label="μμΉ μ’
λͺ©"), gr.File(label="μμ
νμΌ λ€μ΄λ‘λ")], # HTML μΆλ ₯μ ν΅ν μμ νμ λ° μμ
λ€μ΄λ‘λ
|
| 69 |
title="λ€μ΄λ² μ¦κΆ μμΉ μ’
λͺ© μ€ν¬λν",
|
| 70 |
description="λ€μ΄λ² μ¦κΆμμ νμ¬ μμΉ μ€μΈ μμ μ’
λͺ©μ μ€ν¬λννμ¬ νλ‘ λ³΄μ¬μ£Όκ³ , μμ
νμΌλ‘ μ μ₯ν©λλ€.")
|
| 71 |
iface.launch()
|