Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,338 +1,133 @@
|
|
| 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 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
# 定義字體
|
| 135 |
-
header_font = Font(bold=True, color="FFFFFF")
|
| 136 |
-
normal_font = Font(color="000000")
|
| 137 |
-
|
| 138 |
-
# 定義對齊
|
| 139 |
-
center_alignment = Alignment(horizontal="center", vertical="center")
|
| 140 |
-
|
| 141 |
-
# 定義邊框
|
| 142 |
-
thin_border = Border(
|
| 143 |
-
left=Side(style='thin'),
|
| 144 |
-
right=Side(style='thin'),
|
| 145 |
-
top=Side(style='thin'),
|
| 146 |
-
bottom=Side(style='thin')
|
| 147 |
-
)
|
| 148 |
-
|
| 149 |
-
# 設定標題行格式
|
| 150 |
-
for col_num, col_name in enumerate(['編號', '公司代碼', '公司名稱', '公司網址'], 1):
|
| 151 |
-
cell = worksheet.cell(row=1, column=col_num)
|
| 152 |
-
cell.font = header_font
|
| 153 |
-
cell.alignment = center_alignment
|
| 154 |
-
cell.border = thin_border
|
| 155 |
-
|
| 156 |
-
if col_name in ['編號', '公司代碼', '公司名稱']:
|
| 157 |
-
cell.fill = blue_fill
|
| 158 |
-
else:
|
| 159 |
-
cell.fill = gray_fill
|
| 160 |
-
|
| 161 |
-
# 設定資料行格式
|
| 162 |
-
for row_num in range(2, len(df) + 2):
|
| 163 |
-
for col_num in range(1, 5):
|
| 164 |
-
cell = worksheet.cell(row=row_num, column=col_num)
|
| 165 |
-
cell.font = normal_font
|
| 166 |
-
cell.alignment = center_alignment
|
| 167 |
-
cell.border = thin_border
|
| 168 |
-
|
| 169 |
-
# 針對編號、公司代碼、公司名稱欄位設定交替顏色
|
| 170 |
-
if col_num <= 3: # 編號、公司代碼、公司名稱
|
| 171 |
-
if (row_num - 2) % 2 == 0: # 偶數行
|
| 172 |
-
cell.fill = yellow_fill
|
| 173 |
-
else: # 奇數行
|
| 174 |
-
cell.fill = white_fill
|
| 175 |
-
|
| 176 |
-
output.seek(0)
|
| 177 |
-
return output.getvalue()
|
| 178 |
-
|
| 179 |
-
def save_to_csv(df: pd.DataFrame) -> str:
|
| 180 |
-
"""儲存為CSV格式,返回CSV字串"""
|
| 181 |
-
return df.to_csv(index=False, encoding="utf-8-sig")
|
| 182 |
-
|
| 183 |
-
def main():
|
| 184 |
-
st.set_page_config(
|
| 185 |
-
page_title="台灣證交所公司資料爬取工具",
|
| 186 |
-
page_icon="🏢",
|
| 187 |
-
layout="wide",
|
| 188 |
-
initial_sidebar_state="expanded"
|
| 189 |
-
)
|
| 190 |
-
|
| 191 |
-
st.title("🏢 台灣證交所公司資料爬取工具")
|
| 192 |
-
st.markdown("這個工具可以幫您從台灣證交所網站爬取上市公司資料,並提供CSV或Excel格式下載。")
|
| 193 |
-
|
| 194 |
-
# 側邊欄參數設定
|
| 195 |
-
with st.sidebar:
|
| 196 |
-
st.header("⚙️ 參數設定")
|
| 197 |
-
|
| 198 |
-
start_offset = st.number_input(
|
| 199 |
-
"起始偏移量",
|
| 200 |
-
min_value=0,
|
| 201 |
-
value=0,
|
| 202 |
-
step=1,
|
| 203 |
-
help="從第幾筆資料開始爬取"
|
| 204 |
-
)
|
| 205 |
-
|
| 206 |
-
max_per = st.slider(
|
| 207 |
-
"每頁筆數",
|
| 208 |
-
min_value=1,
|
| 209 |
-
max_value=100,
|
| 210 |
-
value=30,
|
| 211 |
-
step=1,
|
| 212 |
-
help="每次請求爬取的資料筆數"
|
| 213 |
-
)
|
| 214 |
-
|
| 215 |
-
max_pages = st.slider(
|
| 216 |
-
"最大頁數",
|
| 217 |
-
min_value=1,
|
| 218 |
-
max_value=100,
|
| 219 |
-
value=50,
|
| 220 |
-
step=1,
|
| 221 |
-
help="最多爬取幾頁資料"
|
| 222 |
-
)
|
| 223 |
-
|
| 224 |
-
output_format = st.radio(
|
| 225 |
-
"輸出格式",
|
| 226 |
-
options=["CSV", "Excel", "兩者都要"],
|
| 227 |
-
index=1,
|
| 228 |
-
help="選擇要下載的檔案格式"
|
| 229 |
-
)
|
| 230 |
-
|
| 231 |
-
st.markdown("---")
|
| 232 |
-
|
| 233 |
-
# 使用說明
|
| 234 |
-
with st.expander("📖 使用說明"):
|
| 235 |
-
st.markdown("""
|
| 236 |
-
### 參數說明:
|
| 237 |
-
- **起始偏移量**:從第幾筆資料開始爬取,通常設為0
|
| 238 |
-
- **每頁筆數**:每次API請求的資料筆數,建議30-50
|
| 239 |
-
- **最大頁數**:最多爬取幾頁,避免設定太大導致執行時間過長
|
| 240 |
-
- **輸出格式**:
|
| 241 |
-
- CSV:純文字格式,適合後續程式處理
|
| 242 |
-
- Excel:包含樣式格式的Excel檔案
|
| 243 |
-
- 兩者都要:同時產生CSV和Excel檔案
|
| 244 |
-
|
| 245 |
-
### 注意事項:
|
| 246 |
-
- 爬取過程中請勿關閉瀏覽器
|
| 247 |
-
- 建議先用較小的參數測試
|
| 248 |
-
- 檔案會自動加上時間戳記避免重複
|
| 249 |
-
""")
|
| 250 |
-
|
| 251 |
-
# 主要內容區域
|
| 252 |
-
col1, col2 = st.columns([2, 1])
|
| 253 |
-
|
| 254 |
-
with col2:
|
| 255 |
-
start_scraping = st.button("🚀 開始爬取", type="primary", use_container_width=True)
|
| 256 |
-
|
| 257 |
-
# 執行爬取
|
| 258 |
-
if start_scraping:
|
| 259 |
-
# 驗證輸入參數
|
| 260 |
-
if start_offset < 0:
|
| 261 |
-
st.error("起始偏移量不能小於0")
|
| 262 |
-
return
|
| 263 |
-
if max_per <= 0 or max_per > 100:
|
| 264 |
-
st.error("每頁筆數必須在1-100之間")
|
| 265 |
-
return
|
| 266 |
-
if max_pages <= 0 or max_pages > 1000:
|
| 267 |
-
st.error("最大頁數必須在1-1000之間")
|
| 268 |
-
return
|
| 269 |
-
|
| 270 |
-
try:
|
| 271 |
-
# 建立進度條和狀態顯示
|
| 272 |
-
progress_bar = st.progress(0)
|
| 273 |
-
status_text = st.empty()
|
| 274 |
-
|
| 275 |
-
# 開始爬取資料
|
| 276 |
-
status_text.text("開始爬取資料...")
|
| 277 |
-
df = collect_all(start_offset, max_per, max_pages, progress_bar, status_text)
|
| 278 |
-
|
| 279 |
-
if df.empty:
|
| 280 |
-
st.warning("未爬取到任何資料")
|
| 281 |
-
return
|
| 282 |
-
|
| 283 |
-
# 產生時間戳記
|
| 284 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 285 |
-
|
| 286 |
-
# 完成狀態
|
| 287 |
-
progress_bar.progress(1.0)
|
| 288 |
-
status_text.text(f"✅ 成功爬取 {len(df)} 筆公司資料!")
|
| 289 |
-
|
| 290 |
-
# 顯示資料預覽
|
| 291 |
-
st.subheader("📊 資料預覽(前10筆)")
|
| 292 |
-
st.dataframe(df.head(10), use_container_width=True)
|
| 293 |
-
|
| 294 |
-
# 檔案下載區域
|
| 295 |
-
st.subheader("📁 檔案下載")
|
| 296 |
-
|
| 297 |
-
download_col1, download_col2 = st.columns(2)
|
| 298 |
-
|
| 299 |
-
if output_format in ["CSV", "兩者都要"]:
|
| 300 |
-
csv_data = save_to_csv(df)
|
| 301 |
-
with download_col1:
|
| 302 |
-
st.download_button(
|
| 303 |
-
label="⬇️ 下載 CSV 檔案",
|
| 304 |
-
data=csv_data,
|
| 305 |
-
file_name=f"companies_{timestamp}.csv",
|
| 306 |
-
mime="text/csv",
|
| 307 |
-
use_container_width=True
|
| 308 |
-
)
|
| 309 |
-
|
| 310 |
-
if output_format in ["Excel", "兩者都要"]:
|
| 311 |
-
excel_data = save_to_excel(df)
|
| 312 |
-
with download_col2:
|
| 313 |
-
st.download_button(
|
| 314 |
-
label="⬇️ 下載 Excel 檔案",
|
| 315 |
-
data=excel_data,
|
| 316 |
-
file_name=f"companies_styled_{timestamp}.xlsx",
|
| 317 |
-
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
| 318 |
-
use_container_width=True
|
| 319 |
-
)
|
| 320 |
-
|
| 321 |
-
# 顯示統計資訊
|
| 322 |
-
st.subheader("📈 統計資訊")
|
| 323 |
-
stat_col1, stat_col2, stat_col3 = st.columns(3)
|
| 324 |
-
|
| 325 |
-
with stat_col1:
|
| 326 |
-
st.metric("總公司數量", len(df))
|
| 327 |
-
|
| 328 |
-
with stat_col2:
|
| 329 |
-
st.metric("有網址的公司", len(df[df['公司網址'] != '']))
|
| 330 |
-
|
| 331 |
-
with stat_col3:
|
| 332 |
-
st.metric("執行頁數", min(max_pages, (len(df) // max_per) + 1))
|
| 333 |
-
|
| 334 |
-
except Exception as e:
|
| 335 |
-
st.error(f"❌ 爬取過程中發生錯誤:{str(e)}")
|
| 336 |
-
|
| 337 |
-
if __name__ == "__main__":
|
| 338 |
-
main()
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="zh-TW">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Hugging Face System</title>
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
font-family: Arial, sans-serif;
|
| 10 |
+
margin: 0;
|
| 11 |
+
padding: 20px;
|
| 12 |
+
background-color: #f4f4f4;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
.tab-container {
|
| 16 |
+
width: 100%;
|
| 17 |
+
max-width: 900px; /* 根據 iframe 寬度調整 */
|
| 18 |
+
margin: 0 auto;
|
| 19 |
+
background: #fff;
|
| 20 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 21 |
+
border-radius: 8px;
|
| 22 |
+
overflow: hidden;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
.tab-header {
|
| 26 |
+
display: flex;
|
| 27 |
+
justify-content: flex-end; /* 將頁籤容器內容靠右對齊 */
|
| 28 |
+
list-style: none;
|
| 29 |
+
padding: 0;
|
| 30 |
+
margin: 0;
|
| 31 |
+
border-bottom: 2px solid #ccc;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
.tab-header li {
|
| 35 |
+
padding: 10px 15px;
|
| 36 |
+
cursor: pointer;
|
| 37 |
+
border: 1px solid transparent;
|
| 38 |
+
border-bottom: none;
|
| 39 |
+
margin-bottom: -2px; /* 使邊框與下劃線重疊 */
|
| 40 |
+
transition: background-color 0.3s, color 0.3s;
|
| 41 |
+
font-weight: bold;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
.tab-header li:hover {
|
| 45 |
+
background-color: #eee;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
.tab-header li.active {
|
| 49 |
+
color: #007bff;
|
| 50 |
+
border-color: #ccc;
|
| 51 |
+
border-bottom: 2px solid #fff; /* 覆蓋下邊線,形成選中效果 */
|
| 52 |
+
background-color: #fff;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
.tab-content {
|
| 56 |
+
padding: 0px;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
.tab-pane {
|
| 60 |
+
display: none; /* 默認隱藏所有內容 */
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
.tab-pane.active {
|
| 64 |
+
display: block; /* 顯示選中的內容 */
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
/* 調整 iframe 樣式 */
|
| 68 |
+
.tab-pane iframe {
|
| 69 |
+
display: block;
|
| 70 |
+
margin: 0 auto; /* 使 iframe 居中 */
|
| 71 |
+
}
|
| 72 |
+
</style>
|
| 73 |
+
</head>
|
| 74 |
+
<body>
|
| 75 |
+
|
| 76 |
+
<div class="tab-container">
|
| 77 |
+
<ul class="tab-header">
|
| 78 |
+
<li class="tab-link active" data-tab="tab-1">多圖片文字辨識工具(Groq API|CSV/DOCX)</li>
|
| 79 |
+
<li class="tab-link" data-tab="tab-2">ESG爬蟲</li>
|
| 80 |
+
</ul>
|
| 81 |
+
|
| 82 |
+
<div class="tab-content">
|
| 83 |
+
<div id="tab-1" class="tab-pane active">
|
| 84 |
+
<iframe
|
| 85 |
+
src="https://123sabrina-mmml-2025.hf.space"
|
| 86 |
+
frameborder="0"
|
| 87 |
+
width="850"
|
| 88 |
+
height="450"
|
| 89 |
+
></iframe>
|
| 90 |
+
</div>
|
| 91 |
+
|
| 92 |
+
<div id="tab-2" class="tab-pane">
|
| 93 |
+
<iframe
|
| 94 |
+
src="https://roberta2024-0825esgscraper.hf.space"
|
| 95 |
+
frameborder="0"
|
| 96 |
+
width="850"
|
| 97 |
+
height="450"
|
| 98 |
+
></iframe>
|
| 99 |
+
</div>
|
| 100 |
+
</div>
|
| 101 |
+
</div>
|
| 102 |
+
|
| 103 |
+
<script>
|
| 104 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 105 |
+
const tabs = document.querySelectorAll('.tab-link');
|
| 106 |
+
const tabPanes = document.querySelectorAll('.tab-pane');
|
| 107 |
+
|
| 108 |
+
tabs.forEach(tab => {
|
| 109 |
+
tab.addEventListener('click', () => {
|
| 110 |
+
const targetTab = tab.getAttribute('data-tab');
|
| 111 |
+
|
| 112 |
+
// 移除所有頁籤的 active 類別
|
| 113 |
+
tabs.forEach(t => t.classList.remove('active'));
|
| 114 |
+
|
| 115 |
+
// 隱藏所有內容面板
|
| 116 |
+
tabPanes.forEach(pane => pane.classList.remove('active'));
|
| 117 |
+
|
| 118 |
+
// 設置當前點擊的頁籤為 active
|
| 119 |
+
tab.classList.add('active');
|
| 120 |
+
|
| 121 |
+
// 顯示對應的內容面板
|
| 122 |
+
document.getElementById(targetTab).classList.add('active');
|
| 123 |
+
});
|
| 124 |
+
});
|
| 125 |
+
|
| 126 |
+
// 默認激活第一個頁籤
|
| 127 |
+
// 在 HTML 中已設置第一個頁籤和內容為 active,這段代碼可以保險起見保留,但目前非必要
|
| 128 |
+
// document.querySelector('.tab-link').click();
|
| 129 |
+
});
|
| 130 |
+
</script>
|
| 131 |
+
|
| 132 |
+
</body>
|
| 133 |
+
</html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|