Spaces:
Running
Running
| 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() | |