Spaces:
Sleeping
Sleeping
File size: 2,749 Bytes
fd35c9f 75b6b1a fd35c9f 75b6b1a 73970f3 75b6b1a 0a6228e fd35c9f 75b6b1a fd35c9f 75b6b1a b3b13e7 75b6b1a b3b13e7 75b6b1a fd35c9f 75b6b1a 0a6228e fd35c9f 75b6b1a fd35c9f 75b6b1a b3b13e7 fd35c9f 75b6b1a |
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 |
import requests
from bs4 import BeautifulSoup
import pandas as pd
import gradio as gr
# λ€μ΄λ² μ½μ€λ₯ URL
KOSDAQ_URL = "https://finance.naver.com/sise/sise_rise.naver?sosok=1"
def scrape_kosdaq_data():
print("λλ²κΉ
: λ€μ΄λ² μ¦κΆ νμ΄μ§ μμ² μμ...")
# μμ² ν€λ μΆκ°
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
# νμ΄μ§ μμ²
response = requests.get(KOSDAQ_URL, headers=headers)
if response.status_code == 200:
print("λλ²κΉ
: λ€μ΄λ² μ¦κΆ νμ΄μ§ μμ² μ±κ³΅")
else:
print(f"λλ²κΉ
: μμ² μ€ν¨, μν μ½λ: {response.status_code}")
return pd.DataFrame() # λΉ DataFrame λ°ν
# HTML νμ±
soup = BeautifulSoup(response.text, "html.parser")
print("λλ²κΉ
: ν ν€λ μΆμΆ μμ...")
# νμ ν€λ μΆμΆ
headers = []
header_tags = soup.select("table.type_2 thead tr th")
for tag in header_tags:
header_text = tag.get_text(strip=True)
if header_text: # λΉ κ° μ κ±°
headers.append(header_text)
print(f"λλ²κΉ
: μΆμΆλ ν€λ - {headers}")
print("λλ²κΉ
: ν λ°μ΄ν° μΆμΆ μμ...")
# νμ λ°μ΄ν° μΆμΆ
rows = soup.select("table.type_2 tbody tr")
data = []
for row in rows:
cols = row.find_all("td")
if cols: # λΉ ν μ μΈ
# κ° μ΄μ ν
μ€νΈλ₯Ό μΆμΆ
row_data = [col.get_text(strip=True) for col in cols]
# λ§ν¬κ° μλ κ²½μ° μ’
λͺ©λͺ
μ μΆκ°
link = row.find("a", class_="tltle")
if link:
row_data[1] = link.text.strip()
data.append(row_data)
print(f"λλ²κΉ
: μΆμΆλ λ°μ΄ν° ν μ - {len(data)}")
if data:
print(f"λλ²κΉ
: 첫 λ²μ§Έ λ°μ΄ν° ν - {data[0]}")
else:
print("λλ²κΉ
: λ°μ΄ν° μΆμΆ μ€ν¨")
return pd.DataFrame(columns=headers) # λΉ DataFrame λ°ν
# DataFrame μμ±
df = pd.DataFrame(data, columns=headers)
print("λλ²κΉ
: DataFrame μμ± μλ£")
return df
def display_kosdaq_info():
# λ°μ΄ν° μ€ν¬λν λ° λ°ν
df = scrape_kosdaq_data()
if not df.empty:
return df
else:
return pd.DataFrame({"κ²°κ³Ό": ["λ°μ΄ν°λ₯Ό κ°μ Έμ€λ λ° μ€ν¨νμ΅λλ€."]})
# Gradio UI μ€μ
gr.Interface(
fn=display_kosdaq_info,
inputs=None,
outputs="dataframe",
title="μ½μ€λ₯ μ’
λͺ© μ 보 μ€ν¬λνΌ",
description="λ€μ΄λ² μ¦κΆ μ¬μ΄νΈμμ μ½μ€λ₯ μ’
λͺ© μ 보λ₯Ό μ€ν¬λννμ¬ νμν©λλ€."
).launch()
|