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