Spaces:
Sleeping
Sleeping
| import re | |
| import requests | |
| import pandas as pd | |
| import gradio as gr | |
| # ๋๋ฒ๊น : ์คํฌ๋ํ ๊ณผ์ ์ ๋ก๊ทธ๋ก ํ์ธํ๊ธฐ ์ํจ | |
| def debug_print(*args): | |
| print("[DEBUG]", *args) | |
| def scrape_data(market_type: str): | |
| """ | |
| market_type (str): '0' -> ์ฝ์คํผ, '1' -> ์ฝ์ค๋ฅ | |
| ๋ค์ด๋ฒ ์ฆ๊ถ์ '์์น ์ข ๋ชฉ' ํ์ด์ง์์ ํด๋น market_type์ ์ ๋ณด๋ฅผ ์คํฌ๋ํ. | |
| (BeautifulSoup / lxml ์์ด ์ ๊ทํํ์๋ง ์ฌ์ฉ) | |
| """ | |
| # market_type์ ๋ฐ๋ผ URL ์ค์ | |
| # '0'์ด๋ฉด ์ฝ์คํผ, '1'์ด๋ฉด ์ฝ์ค๋ฅ | |
| base_url = "https://finance.naver.com/sise/sise_rise.naver?sosok=" | |
| url = base_url + market_type | |
| debug_print("Requesting URL:", url) | |
| response = requests.get(url) | |
| debug_print("Status Code:", response.status_code) | |
| # HTML ์ ์ฒด ํ ์คํธ | |
| html_text = response.text | |
| # 1) <table class="type_2"> ~ </table> ๊ตฌ๊ฐ ์ถ์ถ | |
| pattern_table = re.compile( | |
| r'<table[^>]*class=["\']type_2["\'][^>]*>(.*?)</table>', | |
| re.DOTALL | re.IGNORECASE | |
| ) | |
| match_table = pattern_table.search(html_text) | |
| if not match_table: | |
| debug_print("Error: Target table not found.") | |
| return [] | |
| table_html = match_table.group(1) | |
| # 2) table ๋ด๋ถ์ <tr> ๋จ์๋ก ๋๋๊ธฐ | |
| pattern_tr = re.compile(r'<tr[^>]*>(.*?)</tr>', re.DOTALL | re.IGNORECASE) | |
| rows = pattern_tr.findall(table_html) | |
| debug_print(f"Found total {len(rows)} <tr> blocks in table.") | |
| data_list = [] | |
| row_count = 0 | |
| for row_html in rows: | |
| # 3) <td> ํ๊ทธ ์ถ์ถ | |
| pattern_td = re.compile(r'<td[^>]*>(.*?)</td>', re.DOTALL | re.IGNORECASE) | |
| cols = pattern_td.findall(row_html) | |
| # ์ ํจํ ๋ฐ์ดํฐ ์ด์ด ์๋ ๊ฒฝ์ฐ(๋๋ ๊ณต๋ฐฑํ ๋ฑ) ์คํต | |
| if len(cols) < 12: | |
| continue | |
| # HTML ํ๊ทธ ์ ๊ฑฐ ํฌํผ ํจ์ | |
| def clean_html(raw_html): | |
| # ๋ชจ๋ ํ๊ทธ ์ ๊ฑฐ | |
| text = re.sub(r'<.*?>', '', raw_html, flags=re.DOTALL) | |
| return text.strip() | |
| rank = clean_html(cols[0]) | |
| name = clean_html(cols[1]) | |
| current = clean_html(cols[2]) | |
| diff = clean_html(cols[3]) | |
| change_rate = clean_html(cols[4]) | |
| volume = clean_html(cols[5]) | |
| buy_price = clean_html(cols[6]) | |
| sell_price = clean_html(cols[7]) | |
| total_buy = clean_html(cols[8]) | |
| total_sell = clean_html(cols[9]) | |
| per_ = clean_html(cols[10]) | |
| roe_ = clean_html(cols[11]) | |
| debug_print(f"[Row {row_count}] rank={rank}, name={name}, current_price={current}") | |
| row_count += 1 | |
| data_list.append({ | |
| "์์": rank, | |
| "์ข ๋ชฉ๋ช ": name, | |
| "ํ์ฌ๊ฐ": current, | |
| "์ ์ผ๋น": diff, | |
| "๋ฑ๋ฝ๋ฅ ": change_rate, | |
| "๊ฑฐ๋๋": volume, | |
| "๋งค์ํธ๊ฐ": buy_price, | |
| "๋งค๋ํธ๊ฐ": sell_price, | |
| "๋งค์์ด์๋": total_buy, | |
| "๋งค๋์ด์๋": total_sell, | |
| "PER": per_, | |
| "ROE": roe_ | |
| }) | |
| return data_list | |
| def make_table(market_choice): | |
| """ | |
| market_choice (str): "์ฝ์คํผ" ๋๋ "์ฝ์ค๋ฅ" | |
| ํด๋น ์ ํ์ ๋ฐ๋ผ scrape_data()๋ฅผ ์คํํ ๋ค DataFrame์ผ๋ก ๋ฐํ. | |
| """ | |
| debug_print(f"Scraping data for market_choice={market_choice}...") | |
| # ์ฌ์ฉ์๊ฐ ์ ํ๋ฐ์ค์์ "์ฝ์คํผ" ์ ํ -> '0' | |
| # ์ฌ์ฉ์๊ฐ ์ ํ๋ฐ์ค์์ "์ฝ์ค๋ฅ" ์ ํ -> '1' | |
| market_type = "0" if market_choice == "์ฝ์คํผ" else "1" | |
| data = scrape_data(market_type) | |
| if not data: | |
| debug_print("No data retrieved or table not found.") | |
| return pd.DataFrame(["๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค์ง ๋ชปํ์ต๋๋ค."]) | |
| debug_print("Scraping done. Converting to DataFrame.") | |
| return pd.DataFrame(data) | |
| def main(): | |
| """ | |
| Gradio ์ธํฐํ์ด์ค ์คํ | |
| """ | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# ๋ค์ด๋ฒ ์ฆ๊ถ ์คํฌ๋ํ : ์ฝ์คํผ / ์ฝ์ค๋ฅ ์ ํ") | |
| # ์ ํ๋ฐ์ค: ์ฝ์คํผ / ์ฝ์ค๋ฅ | |
| market_choice = gr.Dropdown( | |
| label="์ข ๋ชฉ ์ ํ", | |
| choices=["์ฝ์คํผ", "์ฝ์ค๋ฅ"], | |
| value="์ฝ์คํผ" # ๊ธฐ๋ณธ๊ฐ | |
| ) | |
| # ๋ฒํผ๊ณผ ๊ฒฐ๊ณผ์ฉ DataFrame | |
| scrape_btn = gr.Button("๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ") | |
| output_df = gr.DataFrame(label="์คํฌ๋ํ ๊ฒฐ๊ณผ") | |
| # ๋ฒํผ ํด๋ฆญ ์ ์คํฌ๋ํ ํจ์ ํธ์ถ | |
| scrape_btn.click(fn=make_table, | |
| inputs=market_choice, | |
| outputs=output_df) | |
| demo.launch() | |
| if __name__ == "__main__": | |
| main() | |