Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,152 @@
|
|
| 1 |
-
import
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
import pandas as pd
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
-
if not getattr(st, '_is_running_with_streamlit', False):
|
| 9 |
-
sys.exit("์ด ์ฑ์ 'streamlit run app.py' ๋ช
๋ น์ด๋ก ์คํ๋์ด์ผ ํฉ๋๋ค.")
|
| 10 |
|
| 11 |
def scrape_market_data(sosok):
|
| 12 |
"""
|
| 13 |
-
|
| 14 |
"""
|
| 15 |
url = f"https://finance.naver.com/sise/sise_market_sum.naver?sosok={sosok}"
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
"""
|
| 37 |
-
|
| 38 |
-
# ์ผํ์ '%' ์ ๊ฑฐ ํ ์ซ์ ๋ณํ
|
| 39 |
-
num = float(value.replace(',', '').replace('%', ''))
|
| 40 |
-
except:
|
| 41 |
-
return value # ๋ณํ ์คํจ์ ์๋ณธ ๋ฐํ
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
return value
|
| 49 |
-
|
| 50 |
-
def main():
|
| 51 |
-
st.title("๋ค์ด๋ฒ ์ฆ๊ถ ์์น๋ฅ TOP ์ข
๋ชฉ ์คํฌ๋ํ")
|
| 52 |
-
|
| 53 |
-
# ์์ฅ ์ ํ: ์ฝ์คํผ(0) / ์ฝ์ค๋ฅ(1)
|
| 54 |
-
market_option = st.selectbox("์์ฅ ์ ํ", options=[("์ฝ์คํผ", 0), ("์ฝ์ค๋ฅ", 1)], format_func=lambda x: x[0])
|
| 55 |
-
headers, data = scrape_market_data(market_option[1])
|
| 56 |
-
df = pd.DataFrame(data, columns=headers)
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
writer.close() # writer.save() ๋์ close() ์ฌ์ฉ
|
| 72 |
-
|
| 73 |
-
with open(excel_file, "rb") as file:
|
| 74 |
-
st.download_button(label="์์
๋ค์ด๋ก๋", data=file, file_name=excel_file)
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
-
|
|
|
|
| 1 |
+
from flask import Flask, request, render_template_string, send_file
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
import pandas as pd
|
| 5 |
+
from io import BytesIO
|
| 6 |
|
| 7 |
+
app = Flask(__name__)
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def scrape_market_data(sosok):
|
| 10 |
"""
|
| 11 |
+
sosok: "0" for ์ฝ์คํผ, "1" for ์ฝ์ค๋ฅ
|
| 12 |
"""
|
| 13 |
url = f"https://finance.naver.com/sise/sise_market_sum.naver?sosok={sosok}"
|
| 14 |
+
res = requests.get(url)
|
| 15 |
+
# ๋ค์ด๋ฒ ๊ธ์ต์ euc-kr ์ธ์ฝ๋ฉ ์ฌ์ฉ
|
| 16 |
+
res.encoding = "euc-kr"
|
| 17 |
+
soup = BeautifulSoup(res.text, "html.parser")
|
| 18 |
+
# ๋ฐ์ดํฐ๋ table.type_2 ์์ tr ํ๊ทธ๋ค์ ์์ต๋๋ค.
|
| 19 |
+
rows = soup.select("table.type_2 tr")
|
| 20 |
+
data_list = []
|
| 21 |
+
for row in rows:
|
| 22 |
+
cols = row.find_all("td")
|
| 23 |
+
# ์ปฌ๋ผ์ด 2๊ฐ ์ด์์ด๋ฉด ๋ฐ์ดํฐ row๋ก ํ๋จ
|
| 24 |
+
if len(cols) > 1:
|
| 25 |
+
try:
|
| 26 |
+
rank = cols[0].get_text(strip=True)
|
| 27 |
+
# ์ข
๋ชฉ๋ช
์ <a> ํ๊ทธ ๋ด๋ถ์ ์์
|
| 28 |
+
name_elem = cols[1].find("a")
|
| 29 |
+
name = name_elem.get_text(strip=True) if name_elem else cols[1].get_text(strip=True)
|
| 30 |
+
current_price = cols[2].get_text(strip=True)
|
| 31 |
+
|
| 32 |
+
# ์ ์ผ๋น ์นผ๋ผ: ์์ด์ฝ(์์น/ํ๋ฝ ๋ฑ)๊ณผ ๊ฐ์ด ํฌํจ๋์ด ์์.
|
| 33 |
+
diff_td = cols[3]
|
| 34 |
+
em = diff_td.find("em")
|
| 35 |
+
# em ํ๊ทธ์ span.blind ๋ด๋ถ ํ
์คํธ๋ ์์น/ํ๋ฝ/์ํ๊ฐ/ํํ๊ฐ ์ ๋ณด๋ฅผ ์ ๊ณตํฉ๋๋ค.
|
| 36 |
+
indicator = em.find("span", class_="blind").get_text(strip=True) if em else ""
|
| 37 |
+
diff_val_span = diff_td.find("span", class_="tah")
|
| 38 |
+
diff_val = diff_val_span.get_text(strip=True) if diff_val_span else ""
|
| 39 |
+
|
| 40 |
+
change_rate = cols[4].get_text(strip=True)
|
| 41 |
+
|
| 42 |
+
# ์ ์ผ๋น ์์น๋ฅผ ์ซ์๋ก ๋ณํ (์ฝค๋ง ์ ๊ฑฐ)
|
| 43 |
+
try:
|
| 44 |
+
diff_num = float(diff_val.replace(',', ''))
|
| 45 |
+
except:
|
| 46 |
+
diff_num = 0.0
|
| 47 |
+
|
| 48 |
+
# ์์น์ด๋ฉด ๋ถ์์, ํ๋ฝ์ด๋ฉด ํ๋์, ์๋๋ฉด ๊ธฐ๋ณธ์(๊ฒ์ )
|
| 49 |
+
color = "red" if diff_num > 0 else ("blue" if diff_num < 0 else "black")
|
| 50 |
+
|
| 51 |
+
# indicator์ ๋ฐ๋ผ ์์ด์ฝ ๋งคํ (์์ผ๋ฉด ์ซ์ ๋ถํธ๋ก ๊ฒฐ์ )
|
| 52 |
+
icon_map = {"์์น": "โฒ", "ํ๋ฝ": "โผ", "์ํ๊ฐ": "โ", "ํํ๊ฐ": "โ"}
|
| 53 |
+
if indicator in icon_map:
|
| 54 |
+
icon = icon_map[indicator]
|
| 55 |
+
else:
|
| 56 |
+
icon = "โฒ" if diff_num > 0 else ("โผ" if diff_num < 0 else "")
|
| 57 |
+
|
| 58 |
+
data_list.append({
|
| 59 |
+
"rank": rank,
|
| 60 |
+
"name": name,
|
| 61 |
+
"current_price": current_price,
|
| 62 |
+
"diff_val": diff_val,
|
| 63 |
+
"change_rate": change_rate,
|
| 64 |
+
"color": color,
|
| 65 |
+
"icon": icon
|
| 66 |
+
})
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(e)
|
| 69 |
+
continue
|
| 70 |
+
return data_list
|
| 71 |
|
| 72 |
+
@app.route("/", methods=["GET"])
|
| 73 |
+
def index():
|
| 74 |
+
# market ํ๋ผ๋ฏธํฐ๋ก ์ฝ์คํผ(0)์ ์ฝ์ค๋ฅ(1) ์ ํ (๊ธฐ๋ณธ: ์ฝ์คํผ)
|
| 75 |
+
sosok = request.args.get("market", "0")
|
| 76 |
+
if sosok not in ["0", "1"]:
|
| 77 |
+
sosok = "0"
|
| 78 |
+
data_list = scrape_market_data(sosok)
|
| 79 |
+
|
| 80 |
+
# HTML ํ
ํ๋ฆฟ (๊ฐ๋จํ form, ํ
์ด๋ธ, ์์
๋ค์ด๋ก๋ ๋ฒํผ ํฌํจ)
|
| 81 |
+
html = """
|
| 82 |
+
<!DOCTYPE html>
|
| 83 |
+
<html lang="ko">
|
| 84 |
+
<head>
|
| 85 |
+
<meta charset="UTF-8">
|
| 86 |
+
<title>๋ค์ด๋ฒ ์ฆ๊ถ ์์น๋ฅ TOP ์ข
๋ชฉ ์คํฌ๋ํ</title>
|
| 87 |
+
</head>
|
| 88 |
+
<body>
|
| 89 |
+
<h1>๋ค์ด๋ฒ ์ฆ๊ถ ์์น๋ฅ TOP ์ข
๋ชฉ ์คํฌ๋ํ</h1>
|
| 90 |
+
<form method="get">
|
| 91 |
+
<label for="market">์์ฅ ์ ํ:</label>
|
| 92 |
+
<select name="market" id="market">
|
| 93 |
+
<option value="0" {% if market == "0" %}selected{% endif %}>์ฝ์คํผ</option>
|
| 94 |
+
<option value="1" {% if market == "1" %}selected{% endif %}>์ฝ์ค๋ฅ</option>
|
| 95 |
+
</select>
|
| 96 |
+
<button type="submit">์กฐํ</button>
|
| 97 |
+
</form>
|
| 98 |
+
<br>
|
| 99 |
+
<table border="1" cellpadding="5">
|
| 100 |
+
<tr>
|
| 101 |
+
<th>N</th>
|
| 102 |
+
<th>์ข
๋ชฉ๋ช
</th>
|
| 103 |
+
<th>ํ์ฌ๊ฐ</th>
|
| 104 |
+
<th>์ ์ผ๋น</th>
|
| 105 |
+
<th>๋ฑ๋ฝ๋ฅ </th>
|
| 106 |
+
</tr>
|
| 107 |
+
{% for row in data_list %}
|
| 108 |
+
<tr>
|
| 109 |
+
<td>{{ row.rank }}</td>
|
| 110 |
+
<td>{{ row.name }}</td>
|
| 111 |
+
<td>{{ row.current_price }}</td>
|
| 112 |
+
<td style="color: {{ row.color }};">
|
| 113 |
+
{{ row.icon }} {{ row.diff_val }}
|
| 114 |
+
</td>
|
| 115 |
+
<td style="color: {{ row.color }};">
|
| 116 |
+
{{ row.change_rate }}
|
| 117 |
+
</td>
|
| 118 |
+
</tr>
|
| 119 |
+
{% endfor %}
|
| 120 |
+
</table>
|
| 121 |
+
<br>
|
| 122 |
+
<form action="/download" method="get">
|
| 123 |
+
<input type="hidden" name="market" value="{{ market }}">
|
| 124 |
+
<button type="submit">์์
๋ค์ด๋ก๋</button>
|
| 125 |
+
</form>
|
| 126 |
+
</body>
|
| 127 |
+
</html>
|
| 128 |
"""
|
| 129 |
+
return render_template_string(html, data_list=data_list, market=sosok)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
@app.route("/download", methods=["GET"])
|
| 132 |
+
def download_excel():
|
| 133 |
+
sosok = request.args.get("market", "0")
|
| 134 |
+
data_list = scrape_market_data(sosok)
|
| 135 |
+
df = pd.DataFrame(data_list)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
+
# BytesIO ๊ฐ์ฒด์ ์์
์ ์ฅ (openpyxl ์์ง ์ฌ์ฉ)
|
| 138 |
+
output = BytesIO()
|
| 139 |
+
with pd.ExcelWriter(output, engine='openpyxl') as writer:
|
| 140 |
+
df.to_excel(writer, index=False, sheet_name="MarketData")
|
| 141 |
+
output.seek(0)
|
| 142 |
|
| 143 |
+
market_name = "์ฝ์คํผ" if sosok == "0" else "์ฝ์ค๋ฅ"
|
| 144 |
+
return send_file(
|
| 145 |
+
output,
|
| 146 |
+
attachment_filename=f"{market_name}_data.xlsx",
|
| 147 |
+
as_attachment=True,
|
| 148 |
+
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 149 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
if __name__ == "__main__":
|
| 152 |
+
app.run(debug=True)
|