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()