aliceblue11 commited on
Commit
c3fa335
·
verified ·
1 Parent(s): 143348d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py CHANGED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from bs4 import BeautifulSoup
3
+ import pandas as pd
4
+ import gradio as gr
5
+
6
+ # 스크래핑 함수
7
+ def scrape_naver_stock():
8
+ url = "https://finance.naver.com/sise/sise_rise.naver?sosok=1"
9
+ response = requests.get(url)
10
+ response.encoding = 'euc-kr'
11
+
12
+ # BeautifulSoup으로 HTML 파싱
13
+ soup = BeautifulSoup(response.text, 'html.parser')
14
+ table = soup.find('table', class_='type_2')
15
+
16
+ # 테이블에서 데이터 추출
17
+ rows = table.find_all('tr')
18
+ data = []
19
+ for row in rows:
20
+ cols = row.find_all('td')
21
+ if len(cols) > 1:
22
+ rank = cols[0].text.strip()
23
+ name = cols[1].text.strip()
24
+ price = cols[2].text.strip()
25
+ diff = cols[3].text.strip()
26
+ change_rate = cols[4].text.strip()
27
+ volume = cols[5].text.strip()
28
+ buy_price = cols[6].text.strip()
29
+ sell_price = cols[7].text.strip()
30
+ buy_volume = cols[8].text.strip()
31
+ sell_volume = cols[9].text.strip()
32
+ per = cols[10].text.strip()
33
+ roe = cols[11].text.strip()
34
+ data.append([rank, name, price, diff, change_rate, volume, buy_price, sell_price, buy_volume, sell_volume, per, roe])
35
+
36
+ # Pandas DataFrame으로 변환
37
+ df = pd.DataFrame(data, columns=['순위', '종목명', '현재가', '전일비', '등락률', '거래량', '매수호가', '매도호가', '매수총잔량', '매도총잔량', 'PER', 'ROE'])
38
+
39
+ return df
40
+
41
+ # 그라디오 UI
42
+ def display_stocks():
43
+ df = scrape_naver_stock()
44
+ return df
45
+
46
+ iface = gr.Interface(fn=display_stocks, inputs=[], outputs="dataframe")
47
+ iface.launch()