Spaces:
Running
Running
Upload 2 files
Browse files- app (28).py +82 -0
- requirements (9).txt +4 -0
app (28).py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
import pandas as pd
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
def fetch_kosdaq_data():
|
7 |
+
# ๋ค์ด๋ฒ ์ฆ๊ถ ์ฝ์ค๋ฅ URL
|
8 |
+
url = "https://finance.naver.com/sise/sise_rise.naver?sosok=1"
|
9 |
+
|
10 |
+
try:
|
11 |
+
# ์น ํ์ด์ง ์์ฒญ
|
12 |
+
response = requests.get(url)
|
13 |
+
response.raise_for_status()
|
14 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
15 |
+
|
16 |
+
# ํ
์ด๋ธ ๋ฐ์ดํฐ ์ถ์ถ
|
17 |
+
table = soup.find("table", class_="type_2")
|
18 |
+
rows = table.find_all("tr")
|
19 |
+
|
20 |
+
data = []
|
21 |
+
for row in rows:
|
22 |
+
columns = row.find_all("td")
|
23 |
+
if len(columns) >= 12: # 12๊ฐ ์ด์ด ์๋ ํ๋ง ์ฒ๋ฆฌ
|
24 |
+
try:
|
25 |
+
# ๋ฐ์ดํฐ ํ์ฑ
|
26 |
+
rank = columns[0].get_text(strip=True)
|
27 |
+
name = columns[1].get_text(strip=True)
|
28 |
+
current_price = columns[2].get_text(strip=True)
|
29 |
+
diff = columns[3].get_text(strip=True)
|
30 |
+
change_rate = columns[4].get_text(strip=True)
|
31 |
+
volume = columns[5].get_text(strip=True)
|
32 |
+
buy_price = columns[6].get_text(strip=True)
|
33 |
+
sell_price = columns[7].get_text(strip=True)
|
34 |
+
buy_total = columns[8].get_text(strip=True)
|
35 |
+
sell_total = columns[9].get_text(strip=True)
|
36 |
+
per = columns[10].get_text(strip=True)
|
37 |
+
roe = columns[11].get_text(strip=True)
|
38 |
+
|
39 |
+
data.append([
|
40 |
+
rank, name, current_price, diff, change_rate,
|
41 |
+
volume, buy_price, sell_price, buy_total,
|
42 |
+
sell_total, per, roe
|
43 |
+
])
|
44 |
+
except Exception as e:
|
45 |
+
print(f"Error parsing row: {e}")
|
46 |
+
continue
|
47 |
+
|
48 |
+
# DataFrame ์์ฑ
|
49 |
+
columns = ["Rank", "Name", "Current Price", "Difference", "Change Rate",
|
50 |
+
"Volume", "Buy Price", "Sell Price", "Buy Total",
|
51 |
+
"Sell Total", "PER", "ROE"]
|
52 |
+
df = pd.DataFrame(data, columns=columns)
|
53 |
+
return df
|
54 |
+
|
55 |
+
except Exception as e:
|
56 |
+
print(f"Error occurred: {e}")
|
57 |
+
return None
|
58 |
+
|
59 |
+
def display_data():
|
60 |
+
df = fetch_kosdaq_data()
|
61 |
+
if df is not None and not df.empty:
|
62 |
+
return df
|
63 |
+
else:
|
64 |
+
return "Failed to fetch data or no data available. Please check the logs."
|
65 |
+
|
66 |
+
# Gradio ์ธํฐํ์ด์ค ์ค์
|
67 |
+
def gradio_interface():
|
68 |
+
with gr.Blocks() as demo:
|
69 |
+
gr.Markdown("# ๋ค์ด๋ฒ ์ฆ๊ถ ์ฝ์ค๋ฅ ๋ฐ์ดํฐ ์คํฌ๋ํ")
|
70 |
+
fetch_button = gr.Button("๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ")
|
71 |
+
output_table = gr.Dataframe(headers=["Rank", "Name", "Current Price", "Difference", "Change Rate",
|
72 |
+
"Volume", "Buy Price", "Sell Price", "Buy Total",
|
73 |
+
"Sell Total", "PER", "ROE"]) # ๋ช
์์ ์ด ์ด๋ฆ ์ง์
|
74 |
+
|
75 |
+
fetch_button.click(fn=fetch_kosdaq_data, inputs=[], outputs=output_table)
|
76 |
+
|
77 |
+
return demo
|
78 |
+
|
79 |
+
demo = gradio_interface()
|
80 |
+
|
81 |
+
if __name__ == "__main__":
|
82 |
+
demo.launch()
|
requirements (9).txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
beautifulsoup4==4.12.2
|
2 |
+
pandas==1.5.3
|
3 |
+
requests==2.31.0
|
4 |
+
gradio==3.41.0
|