File size: 2,891 Bytes
2aa5925 5162f1f 0537d33 50be270 ef193bf 5162f1f 0537d33 ef193bf 0537d33 5162f1f ef193bf 5162f1f 2aa5925 0537d33 2aa5925 5162f1f ef193bf 5162f1f ef193bf e435d5c ef193bf e435d5c 50be270 e435d5c 50be270 e435d5c 50be270 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
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
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
# BeautifulSoup์ ์ฌ์ฉํ์ฌ HTML ํ์ฑ
soup = BeautifulSoup(html, 'html.parser')
# ํ
์ด๋ธ ์ฐพ๊ธฐ
table = soup.find("table", {"class": "type_2"})
# ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ๋ฆฌ์คํธ
data = []
# ํ
์ด๋ธ์ ๋ชจ๋ ํ(tr) ๊ฐ์ ธ์ค๊ธฐ
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])
# DataFrame์ผ๋ก ๋ณํ
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)
# ์์
ํ์ผ์ ์คํ์ผ ์ถ๊ฐ (์์น๋ฅ ์ด 20% ์ด์์ด๋ฉด ์ข
๋ชฉ๋ช
์ ๋นจ๊ฐ์์ผ๋ก ํ์)
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
# Gradio ์ธํฐํ์ด์ค ์ ์ (ํ๋ฉด ํ์ ๋ฐ ํ์ผ ๋ค์ด๋ก๋ ๋งํฌ ์ ๊ณต)
def get_top_stocks():
df, file_path = scrape_naver_finance()
return df, file_path
# Gradio ์ธํฐํ์ด์ค ์์ฑ (DataFrame๊ณผ ํ์ผ ๋ค์ด๋ก๋ ์ ๊ณต)
iface = gr.Interface(fn=get_top_stocks, inputs=None, outputs=["dataframe", "file"], title="๋ค์ด๋ฒ ์ฆ๊ถ ์์น TOP ์ข
๋ชฉ")
iface.launch()
|