Spaces:
Sleeping
Sleeping
File size: 3,339 Bytes
62047e7 40404d4 90b5a8a 62047e7 90b5a8a 62047e7 64949c0 62047e7 64949c0 62047e7 90b5a8a 62047e7 90b5a8a 40404d4 7d126ce 65494ca 7d126ce 65494ca 7d126ce 65494ca 90b5a8a 62047e7 40404d4 90b5a8a 40404d4 7b1c6fe 40404d4 62047e7 90b5a8a 62047e7 40404d4 b8dd41f 5f960fc 90b5a8a b8dd41f 5f960fc b8dd41f 40404d4 62047e7 b8dd41f |
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 requests
from bs4 import BeautifulSoup
import pandas as pd
from io import BytesIO
import gradio as gr
# λ€μ΄λ² μ¦κΆ μμΉ μ’
λͺ© μ€ν¬λν ν¨μ
def scrape_naver_stock():
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:
cols = row.find_all('td')
if len(cols) > 1: # μ€μ λ°μ΄ν°κ° μλ νλ§ μ²λ¦¬
rank = cols[0].text.strip() # μμ
name = cols[1].text.strip() # μ’
λͺ©λͺ
current_price = cols[2].text.strip() # νμ¬κ°
change_price = cols[3].text.strip() # μ μΌλΉ
change_rate = cols[4].text.strip() # λ±λ½λ₯
volume = cols[5].text.strip() # κ±°λλ
# μμΉ, νλ½, μνκ°λ₯Ό ꡬλΆνμ¬ νμ΄ν μ΄λͺ¨μ§ μΆκ°
if '+' in change_rate:
if '30.00%' in change_rate:
change_rate = 'πΊ ' + change_rate # μνκ°
else:
change_rate = 'π΄β¬ ' + change_rate # μμΉ
elif '-' in change_rate:
change_rate = 'π΅β¬ ' + change_rate # νλ½
stock_data.append([rank, name, current_price, change_price, change_rate, volume])
return stock_data
# λ°μ΄ν°νλ μμ μμ
νμΌλ‘ λ³ννλ ν¨μ
def save_to_excel(stock_data):
df = pd.DataFrame(stock_data, columns=["μμ", "μ’
λͺ©λͺ
", "νμ¬κ°", "μ μΌλΉ", "λ±λ½λ₯ ", "κ±°λλ"])
output = BytesIO()
writer = pd.ExcelWriter(output, engine='openpyxl')
df.to_excel(writer, index=False, sheet_name='λ€μ΄λ² μ¦κΆ μμΉ μ’
λͺ©')
writer.save()
writer.close() # μμ
νμΌ μ μ₯ ν writerλ₯Ό λ«μ
output.seek(0)
return output
# Gradio μΈν°νμ΄μ€μμ λ°μ΄ν°λ₯Ό λ°ννλ ν¨μ
def display_stocks():
stock_data = scrape_naver_stock()
return pd.DataFrame(stock_data, columns=["μμ", "μ’
λͺ©λͺ
", "νμ¬κ°", "μ μΌλΉ", "λ±λ½λ₯ ", "κ±°λλ"])
# μμ
νμΌλ‘ λ³ν λ° λ€μ΄λ‘λλ₯Ό μ 곡νλ ν¨μ
def download_excel():
stock_data = scrape_naver_stock()
excel_file = save_to_excel(stock_data)
return excel_file
# Gradio Blocks λ΄μμ UI λ° μ΄λ²€νΈ μ€μ
with gr.Blocks() as app:
# μμΉ μ’
λͺ©μ νμν DataFrame μ»΄ν¬λνΈ
stock_table = gr.DataFrame(label="λ€μ΄λ² μ¦κΆ μμΉ TOP μ’
λͺ©", headers=["μμ", "μ’
λͺ©λͺ
", "νμ¬κ°", "μ μΌλΉ", "λ±λ½λ₯ ", "κ±°λλ"])
# μμ
λ€μ΄λ‘λ λ²νΌ
download_button = gr.Button("μμ
λ‘ λ€μ΄λ‘λ")
# μμ
νμΌ λ€μ΄λ‘λλ₯Ό μν νμΌ μ»΄ν¬λνΈ
download_file = gr.File(label="μμ
νμΌ λ€μ΄λ‘λ")
# λ²νΌ ν΄λ¦ μ μ΄λ²€νΈ μ°κ²°
stock_button = gr.Button("μ’
λͺ© λ°μ΄ν° μ
λ°μ΄νΈ")
stock_button.click(fn=display_stocks, inputs=None, outputs=stock_table)
download_button.click(fn=download_excel, inputs=None, outputs=download_file)
# μ€ν
if __name__ == "__main__":
app.launch() |