aliceblue11 commited on
Commit
7ae63a6
·
verified ·
1 Parent(s): e91e436

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -60
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
- # Set up logging
8
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
9
- logger = logging.getLogger()
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
- if response.status_code != 200:
21
- logger.error("Failed to fetch data. Status code: %d", response.status_code)
22
- return f"Error: Unable to fetch data (status code: {response.status_code})"
 
 
23
 
24
- soup = BeautifulSoup(response.content, 'html.parser')
 
 
25
 
26
- table = soup.find("table", {"class": "type_2"})
27
- if not table:
28
- logger.error("Failed to find the data table in the HTML.")
29
- return "Error: Unable to find the data table."
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- rows = table.find_all("tr")
32
- data = []
33
-
34
- for row in rows:
35
- cols = row.find_all("td")
36
- cols = [col.text.strip() for col in cols]
37
- if len(cols) > 1:
38
- data.append(cols)
39
 
40
- if not data:
41
- logger.error("No data found in the table.")
42
- return "Error: No data found in the table."
 
 
 
43
 
44
- # Define column names based on the structure
45
- columns = [
46
- "N", "종목명", "현재가", "전일비", "등락률", "거래량", "거래대금", "매수호가",
47
- "매도호가", "시가총액", "PER", "ROE"
48
- ]
49
 
50
- df = pd.DataFrame(data, columns=columns[:len(data[0])])
51
-
52
- logger.info("Successfully scraped data with %d rows.", len(df))
53
- return df
 
 
54
 
55
- def interface_function():
56
- try:
57
- df = scrape_kosdaq()
58
- if isinstance(df, str): # If error message
59
- return df
 
60
 
61
- logger.info("Displaying scraped data to user.")
62
- return df.to_csv(index=False)
63
- except Exception as e:
64
- logger.exception("An error occurred during scraping.")
65
- return f"An error occurred: {str(e)}"
66
 
67
- def display_interface():
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
- display_interface()
 
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()