Spaces:
Build error
Build error
Download app.py from SG34/kosdak_scraper: direct link, hf CLI and curl.
- Browser
- Download file 4.18 kB
-
https://huggingface.co/spaces/SG34/kosdak_scraper/resolve/main/app.py
- Command line
-
hf download hf://spaces/SG34/kosdak_scraper/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SG34/kosdak_scraper/resolve/main/app.py
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() | |