File size: 3,131 Bytes
4c853de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import requests
from bs4 import BeautifulSoup
import pandas as pd
import gradio as gr

def fetch_kosdaq_data():
    # ๋„ค์ด๋ฒ„ ์ฆ๊ถŒ ์ฝ”์Šค๋‹ฅ URL
    url = "https://finance.naver.com/sise/sise_rise.naver?sosok=1"
    
    try:
        # ์›น ํŽ˜์ด์ง€ ์š”์ฒญ
        response = requests.get(url)
        response.raise_for_status()
        soup = BeautifulSoup(response.content, "html.parser")

        # ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ ์ถ”์ถœ
        table = soup.find("table", class_="type_2")
        rows = table.find_all("tr")

        data = []
        for row in rows:
            columns = row.find_all("td")
            if len(columns) >= 12:  # 12๊ฐœ ์—ด์ด ์žˆ๋Š” ํ–‰๋งŒ ์ฒ˜๋ฆฌ
                try:
                    # ๋ฐ์ดํ„ฐ ํŒŒ์‹ฑ
                    rank = columns[0].get_text(strip=True)
                    name = columns[1].get_text(strip=True)
                    current_price = columns[2].get_text(strip=True)
                    diff = columns[3].get_text(strip=True)
                    change_rate = columns[4].get_text(strip=True)
                    volume = columns[5].get_text(strip=True)
                    buy_price = columns[6].get_text(strip=True)
                    sell_price = columns[7].get_text(strip=True)
                    buy_total = columns[8].get_text(strip=True)
                    sell_total = columns[9].get_text(strip=True)
                    per = columns[10].get_text(strip=True)
                    roe = columns[11].get_text(strip=True)

                    data.append([
                        rank, name, current_price, diff, change_rate, 
                        volume, buy_price, sell_price, buy_total, 
                        sell_total, per, roe
                    ])
                except Exception as e:
                    print(f"Error parsing row: {e}")
                    continue

        # DataFrame ์ƒ์„ฑ
        columns = ["Rank", "Name", "Current Price", "Difference", "Change Rate", 
                   "Volume", "Buy Price", "Sell Price", "Buy Total", 
                   "Sell Total", "PER", "ROE"]
        df = pd.DataFrame(data, columns=columns)
        return df

    except Exception as e:
        print(f"Error occurred: {e}")
        return None

def display_data():
    df = fetch_kosdaq_data()
    if df is not None and not df.empty:
        return df
    else:
        return "Failed to fetch data or no data available. Please check the logs."

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
def gradio_interface():
    with gr.Blocks() as demo:
        gr.Markdown("# ๋„ค์ด๋ฒ„ ์ฆ๊ถŒ ์ฝ”์Šค๋‹ฅ ๋ฐ์ดํ„ฐ ์Šคํฌ๋ž˜ํ•‘")
        fetch_button = gr.Button("๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ")
        output_table = gr.Dataframe(headers=["Rank", "Name", "Current Price", "Difference", "Change Rate",
                                             "Volume", "Buy Price", "Sell Price", "Buy Total",
                                             "Sell Total", "PER", "ROE"])  # ๋ช…์‹œ์  ์—ด ์ด๋ฆ„ ์ง€์ •

        fetch_button.click(fn=fetch_kosdaq_data, inputs=[], outputs=output_table)

    return demo

demo = gradio_interface()

if __name__ == "__main__":
    demo.launch()