File size: 2,840 Bytes
5cb8bcc
 
 
 
 
7ae63a6
 
 
5cb8bcc
7ae63a6
 
 
 
 
5cb8bcc
7ae63a6
 
 
5cb8bcc
7ae63a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cb8bcc
7ae63a6
 
 
 
 
5cb8bcc
7ae63a6
 
 
 
 
 
5cb8bcc
7ae63a6
 
 
5cb8bcc
7ae63a6
 
 
 
 
 
5cb8bcc
7ae63a6
 
 
 
 
299ea3e
 
 
5cb8bcc
7ae63a6
 
 
5cb8bcc
7ae63a6
5cb8bcc
 
7ae63a6
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
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) > 0:
                # 데이터 파싱
                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
                ])

        # 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:
        return df
    else:
        return "Failed to fetch data. 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()