| | import os |
| | import requests |
| | from bs4 import BeautifulSoup |
| | import pandas as pd |
| | import re |
| | import gradio as gr |
| | from openpyxl import load_workbook |
| | from openpyxl.styles import Font |
| |
|
| | |
| | url = "https://finance.naver.com/sise/sise_rise.naver?sosok=1" |
| |
|
| | |
| | def scrape_naver_finance(): |
| | |
| | response = requests.get(url) |
| | response.encoding = 'euc-kr' |
| | html = response.text |
| |
|
| | |
| | soup = BeautifulSoup(html, 'html.parser') |
| |
|
| | |
| | table = soup.find("table", {"class": "type_2"}) |
| |
|
| | |
| | data = [] |
| |
|
| | |
| | rows = table.find_all("tr")[2:] |
| |
|
| | for row in rows: |
| | columns = row.find_all("td") |
| | if len(columns) > 1: |
| | rank = columns[0].text.strip() |
| | name = columns[1].text.strip() |
| | price = columns[2].text.strip() |
| | change = columns[3].text.strip() |
| | rate = columns[4].text.strip() |
| | volume = columns[5].text.strip() |
| |
|
| | |
| | change_cleaned = re.sub(r'[^\d-]', '', change).strip() |
| | rate_cleaned = re.sub(r'[^\d.-]', '', rate).strip() |
| |
|
| | |
| | data.append([rank, name, price, change_cleaned, rate_cleaned, volume]) |
| |
|
| | |
| | df = pd.DataFrame(data, columns=["์์", "์ข
๋ชฉ๋ช
", "ํ์ฌ๊ฐ", "์ ์ผ๋น", "๋ฑ๋ฝ๋ฅ ", "๊ฑฐ๋๋"]) |
| |
|
| | |
| | save_dir = "/mnt/data" |
| | if not os.path.exists(save_dir): |
| | os.makedirs(save_dir) |
| |
|
| | |
| | save_path = os.path.join(save_dir, "naver_top_stocks.xlsx") |
| | df.to_excel(save_path, index=False) |
| |
|
| | |
| | workbook = load_workbook(save_path) |
| | sheet = workbook.active |
| | red_font = Font(color="FF0000") |
| |
|
| | for row in range(2, sheet.max_row + 1): |
| | rate_value = sheet.cell(row=row, column=5).value |
| | if rate_value and float(rate_value) >= 20.0: |
| | sheet.cell(row=row, column=2).font = red_font |
| |
|
| | workbook.save(save_path) |
| |
|
| | return df, save_path |
| |
|
| | |
| | def get_top_stocks(): |
| | df, file_path = scrape_naver_finance() |
| | return df, file_path |
| |
|
| | |
| | iface = gr.Interface(fn=get_top_stocks, inputs=None, outputs=["dataframe", "file"], title="๋ค์ด๋ฒ ์ฆ๊ถ ์์น TOP ์ข
๋ชฉ") |
| | iface.launch() |
| |
|