Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,76 +2,75 @@ import requests
|
|
2 |
from bs4 import BeautifulSoup
|
3 |
import pandas as pd
|
4 |
import gradio as gr
|
5 |
-
import logging
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
def scrape_kosdaq():
|
12 |
-
url = "https://finance.naver.com/sise/sise_quant.naver?sosok=1"
|
13 |
-
headers = {
|
14 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
15 |
-
}
|
16 |
-
|
17 |
-
logger.info("Sending request to URL: %s", url)
|
18 |
-
response = requests.get(url, headers=headers)
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
23 |
|
24 |
-
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
cols = [col.text.strip() for col in cols]
|
37 |
-
if len(cols) > 1:
|
38 |
-
data.append(cols)
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
"매도호가", "시가총액", "PER", "ROE"
|
48 |
-
]
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
logger.exception("An error occurred during scraping.")
|
65 |
-
return f"An error occurred: {str(e)}"
|
66 |
|
67 |
-
|
68 |
-
gr.Interface(
|
69 |
-
fn=interface_function,
|
70 |
-
inputs=[],
|
71 |
-
outputs="text",
|
72 |
-
title="네이버 증권 코스닥 스크래퍼",
|
73 |
-
description="네이버 증권에서 코스닥 데이터를 스크래핑하여 CSV 형식으로 반환합니다."
|
74 |
-
).launch()
|
75 |
|
76 |
if __name__ == "__main__":
|
77 |
-
|
|
|
2 |
from bs4 import BeautifulSoup
|
3 |
import pandas as pd
|
4 |
import gradio as gr
|
|
|
5 |
|
6 |
+
def fetch_kosdaq_data():
|
7 |
+
# 네이버 증권 코스닥 URL
|
8 |
+
url = "https://finance.naver.com/sise/sise_rise.naver?sosok=1"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
try:
|
11 |
+
# 웹 페이지 요청
|
12 |
+
response = requests.get(url)
|
13 |
+
response.raise_for_status()
|
14 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
15 |
|
16 |
+
# 테이블 데이터 추출
|
17 |
+
table = soup.find("table", class_="type_2")
|
18 |
+
rows = table.find_all("tr")
|
19 |
|
20 |
+
data = []
|
21 |
+
for row in rows:
|
22 |
+
columns = row.find_all("td")
|
23 |
+
if len(columns) > 0:
|
24 |
+
# 데이터 파싱
|
25 |
+
rank = columns[0].get_text(strip=True)
|
26 |
+
name = columns[1].get_text(strip=True)
|
27 |
+
current_price = columns[2].get_text(strip=True)
|
28 |
+
diff = columns[3].get_text(strip=True)
|
29 |
+
change_rate = columns[4].get_text(strip=True)
|
30 |
+
volume = columns[5].get_text(strip=True)
|
31 |
+
buy_price = columns[6].get_text(strip=True)
|
32 |
+
sell_price = columns[7].get_text(strip=True)
|
33 |
+
buy_total = columns[8].get_text(strip=True)
|
34 |
+
sell_total = columns[9].get_text(strip=True)
|
35 |
+
per = columns[10].get_text(strip=True)
|
36 |
+
roe = columns[11].get_text(strip=True)
|
37 |
|
38 |
+
data.append([
|
39 |
+
rank, name, current_price, diff, change_rate,
|
40 |
+
volume, buy_price, sell_price, buy_total,
|
41 |
+
sell_total, per, roe
|
42 |
+
])
|
|
|
|
|
|
|
43 |
|
44 |
+
# DataFrame 생성
|
45 |
+
columns = ["Rank", "Name", "Current Price", "Difference", "Change Rate",
|
46 |
+
"Volume", "Buy Price", "Sell Price", "Buy Total",
|
47 |
+
"Sell Total", "PER", "ROE"]
|
48 |
+
df = pd.DataFrame(data, columns=columns)
|
49 |
+
return df
|
50 |
|
51 |
+
except Exception as e:
|
52 |
+
print(f"Error occurred: {e}")
|
53 |
+
return None
|
|
|
|
|
54 |
|
55 |
+
def display_data():
|
56 |
+
df = fetch_kosdaq_data()
|
57 |
+
if df is not None:
|
58 |
+
return df
|
59 |
+
else:
|
60 |
+
return "Failed to fetch data. Please check the logs."
|
61 |
|
62 |
+
# Gradio 인터페이스 설정
|
63 |
+
def gradio_interface():
|
64 |
+
with gr.Blocks() as demo:
|
65 |
+
gr.Markdown("# 네이버 증권 코스닥 데이터 스크래핑")
|
66 |
+
fetch_button = gr.Button("데이터 가져오기")
|
67 |
+
output_table = gr.Dataframe(headers="auto")
|
68 |
|
69 |
+
fetch_button.click(fn=fetch_kosdaq_data, inputs=[], outputs=output_table)
|
70 |
+
|
71 |
+
return demo
|
|
|
|
|
72 |
|
73 |
+
demo = gradio_interface()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
if __name__ == "__main__":
|
76 |
+
demo.launch()
|