aliceblue11 commited on
Commit
5cb8bcc
·
verified ·
1 Parent(s): 7542343

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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()