Spaces:
Sleeping
Sleeping
import requests | |
from bs4 import BeautifulSoup | |
import pandas as pd | |
import gradio as gr | |
# 스크래핑 함수 | |
def scrape_naver_stock(): | |
url = "https://finance.naver.com/sise/sise_rise.naver?sosok=1" | |
response = requests.get(url) | |
response.encoding = 'euc-kr' | |
# BeautifulSoup으로 HTML 파싱 | |
soup = BeautifulSoup(response.text, 'html.parser') | |
table = soup.find('table', class_='type_2') | |
# 테이블에서 데이터 추출 | |
rows = table.find_all('tr') | |
data = [] | |
for row in rows: | |
cols = row.find_all('td') | |
if len(cols) > 1: | |
rank = cols[0].text.strip() | |
name = cols[1].text.strip() | |
price = cols[2].text.strip() | |
diff = cols[3].text.strip() | |
change_rate = cols[4].text.strip() | |
volume = cols[5].text.strip() | |
buy_price = cols[6].text.strip() | |
sell_price = cols[7].text.strip() | |
buy_volume = cols[8].text.strip() | |
sell_volume = cols[9].text.strip() | |
per = cols[10].text.strip() | |
roe = cols[11].text.strip() | |
data.append([rank, name, price, diff, change_rate, volume, buy_price, sell_price, buy_volume, sell_volume, per, roe]) | |
# Pandas DataFrame으로 변환 | |
df = pd.DataFrame(data, columns=['순위', '종목명', '현재가', '전일비', '등락률', '거래량', '매수호가', '매도호가', '매수총잔량', '매도총잔량', 'PER', 'ROE']) | |
return df | |
# 그라디오 UI | |
def display_stocks(): | |
df = scrape_naver_stock() | |
return df | |
iface = gr.Interface(fn=display_stocks, inputs=[], outputs="dataframe") | |
iface.launch() | |