NAVER_KOSDAQ / app.py
aliceblue11's picture
Update app.py
de729b3 verified
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()