File size: 2,439 Bytes
fd35c9f
 
 
 
 
3544f83
 
73970f3
3544f83
73970f3
fd35c9f
3544f83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd35c9f
3544f83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd35c9f
 
3544f83
 
 
 
 
 
 
 
 
 
fd35c9f
3544f83
 
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
import requests
from bs4 import BeautifulSoup
import pandas as pd
import gradio as gr

def scrape_kosdaq():
    url = "https://finance.naver.com/sise/sise_rise.naver?sosok=1"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36"
    }

    try:
        # Request the webpage
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        print("[INFO] Page fetched successfully.")

        # Parse the HTML
        soup = BeautifulSoup(response.content, "html.parser")

        # Locate the table
        table = soup.find("table", class_="type_2")
        rows = table.find_all("tr")[2:]  # Skip the header rows

        data = []

        # Extract data row by row
        for row in rows:
            cols = row.find_all("td")
            if len(cols) < 12:  # Skip blank or irrelevant rows
                continue

            entry = {
                "Rank": cols[0].get_text(strip=True),
                "Name": cols[1].get_text(strip=True),
                "Price": cols[2].get_text(strip=True),
                "Change": cols[3].get_text(strip=True),
                "Change_Rate": cols[4].get_text(strip=True),
                "Volume": cols[5].get_text(strip=True),
                "Buy_Price": cols[6].get_text(strip=True),
                "Sell_Price": cols[7].get_text(strip=True),
                "Total_Buy_Quantity": cols[8].get_text(strip=True),
                "Total_Sell_Quantity": cols[9].get_text(strip=True),
                "PER": cols[10].get_text(strip=True),
                "ROE": cols[11].get_text(strip=True),
            }
            data.append(entry)

        print(f"[DEBUG] Extracted {len(data)} rows.")
        return pd.DataFrame(data)

    except requests.exceptions.RequestException as e:
        print(f"[ERROR] Failed to fetch page: {e}")
        return pd.DataFrame()

def display_data():
    df = scrape_kosdaq()
    if df.empty:
        return "Failed to fetch data or no data available."
    return df

# Gradio Interface
def gradio_interface():
    with gr.Blocks() as demo:
        gr.Markdown("### Naver Kosdaq Stock Scraper")
        output = gr.Dataframe()
        fetch_button = gr.Button("Fetch Data")

        fetch_button.click(display_data, inputs=[], outputs=output)

    return demo

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