kosdak_scraper / app.py
SG34's picture
Update app.py
8b3f514 verified
Raw History Blame Contribute Delete
4.18 kB
import requests
from bs4 import BeautifulSoup
import gradio as gr
import pandas as pd
# 넀이버 증ꢌ μƒμŠΉ TOP μ’…λͺ©μ„ μŠ€ν¬λž˜ν•‘ν•˜λŠ” ν•¨μˆ˜
def scrape_naver_stock_rise():
url = "https://finance.naver.com/sise/sise_rise.naver?sosok=1"
response = requests.get(url)
response.encoding = 'euc-kr' # 넀이버 νŽ˜μ΄μ§€λŠ” EUC-KR 인코딩을 μ‚¬μš©
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table', {'class': 'type_2'})
rows = table.find_all('tr')
stock_data = []
for row in rows[2:]: # μƒμœ„ 2개의 rowλŠ” 헀더 및 빈 μΉΈμ΄λ―€λ‘œ μ œμ™Έ
columns = row.find_all('td')
if len(columns) > 1:
rank = int(columns[0].text.strip())
name = columns[1].text.strip()
current_price = int(columns[2].text.replace(",", "").strip())
price_diff_text = columns[3].find("span").text.strip() # 'μƒν•œκ°€', 'μƒμŠΉ' μΆ”μΆœ
price_diff = columns[3].text.strip().replace(",", "").replace('μƒν•œκ°€', '').replace('μƒμŠΉ', '').strip()
price_diff = int(price_diff) if price_diff else 0
change_rate = columns[4].text.strip().replace("%", "").replace(",", "").strip()
change_rate = float(change_rate) if change_rate else 0.0
volume = int(columns[5].text.replace(",", "").strip())
bid_price = int(columns[6].text.replace(",", "").strip())
ask_price = int(columns[7].text.replace(",", "").strip())
bid_total_volume = int(columns[8].text.replace(",", "").strip())
ask_total_volume = int(columns[9].text.replace(",", "").strip())
# PER 처리
per = columns[10].text.strip().replace(",", "")
per = float(per) if per and per != '-' and per != 'N/A' else None
# ROE 처리
roe = columns[11].text.strip().replace(",", "")
roe = float(roe) if roe and roe != '-' and roe != 'N/A' else None
# 가격 변동에 λ”°λ₯Έ 기호 μ„€μ •
if "μƒν•œκ°€" in price_diff_text:
price_diff_symbol = "↑"
elif "μƒμŠΉ" in price_diff_text:
price_diff_symbol = "β–²"
else:
price_diff_symbol = ""
# 가격 변동에 λ”°λ₯Έ 색상 ν‘œμ‹œ
if price_diff > 0:
price_diff = f"<span style='color:red;'>{price_diff}</span>"
change_rate = f"<span style='color:red;'>{change_rate}%</span>"
elif price_diff < 0:
price_diff = f"<span style='color:blue;'>{price_diff}</span>"
change_rate = f"<span style='color:blue;'>{change_rate}%</span>"
else:
change_rate = f"{change_rate}%"
stock_data.append([rank, name, current_price, price_diff_symbol, price_diff, change_rate, volume, bid_price, ask_price, bid_total_volume, ask_total_volume, per, roe])
# DataFrame으둜 λ³€ν™˜
df = pd.DataFrame(stock_data, columns=['μˆœμœ„', 'μ’…λͺ©λͺ…', 'ν˜„μž¬κ°€', '기호', '전일비', '등락λ₯ ', 'κ±°λž˜λŸ‰', 'λ§€μˆ˜ν˜Έκ°€', 'λ§€λ„ν˜Έκ°€', 'λ§€μˆ˜μ΄μž”λŸ‰', 'λ§€λ„μ΄μž”λŸ‰', 'PER', 'ROE'])
return df
# μ—‘μ…€λ‘œ μ €μž₯ν•˜λŠ” ν•¨μˆ˜
def save_to_excel():
df = scrape_naver_stock_rise()
file_path = "naver_stock_rise.xlsx"
df.to_excel(file_path, index=False)
return file_path
# κ·ΈλΌλ””μ˜€ μΈν„°νŽ˜μ΄μŠ€ μ •μ˜
def display_and_save_stocks():
stock_data = scrape_naver_stock_rise()
file_path = save_to_excel()
return stock_data.to_html(escape=False), file_path # escape=False둜 HTML νƒœκ·Έκ°€ μ μš©λ˜λ„λ‘ 함
# Gradio UI μ„€μ •
iface = gr.Interface(fn=display_and_save_stocks,
inputs=[],
outputs=[gr.HTML(label="μƒμŠΉ μ’…λͺ©"), gr.File(label="μ—‘μ…€ 파일 λ‹€μš΄λ‘œλ“œ")], # HTML 좜λ ₯을 ν†΅ν•œ 색상 ν‘œμ‹œ 및 μ—‘μ…€ λ‹€μš΄λ‘œλ“œ
title="넀이버 증ꢌ μƒμŠΉ μ’…λͺ© μŠ€ν¬λž˜ν•‘",
description="넀이버 μ¦κΆŒμ—μ„œ ν˜„μž¬ μƒμŠΉ 쀑인 μƒμœ„ μ’…λͺ©μ„ μŠ€ν¬λž˜ν•‘ν•˜μ—¬ ν‘œλ‘œ 보여주고, μ—‘μ…€ 파일둜 μ €μž₯ν•©λ‹ˆλ‹€.")
iface.launch()