import requests from bs4 import BeautifulSoup import pandas as pd import streamlit as st import plotly.express as px # 定義函數來獲取並處理台南市立醫院的數據 def get_tmh_data(): url_tmh = "https://www.tmh.org.tw/tmh2016/ImpBD.aspx?Kind=2" response_tmh = requests.get(url_tmh) response_tmh.encoding = 'utf-8' soup_tmh = BeautifulSoup(response_tmh.text, 'html.parser') table_tmh = soup_tmh.find('table', {'class': 'RegTable'}) data_tmh = [] rows_tmh = table_tmh.find_all('tr') headers_tmh = [th.text.strip() for th in rows_tmh[0].find_all('th')] for row in rows_tmh[1:]: cols = row.find_all('td') cols = [col.text.strip() for col in cols] data_tmh.append(cols) df_tmh = pd.DataFrame(data_tmh, columns=headers_tmh) # 更改台南市立醫院的列名 df_tmh = df_tmh.rename(columns={ '病床種類': '病床類別', '床位別數': '病床數', '住院人數': '佔床數' }) df_tmh['醫院'] = '台南市立醫院' return df_tmh # 定義函數來獲取並處理奇美醫院的數據 def get_chimei_data(): url_chimei = "https://www.chimei.org.tw/%E4%BD%94%E5%BA%8A%E7%8E%87%E6%9F%A5%E8%A9%A2/%E4%BD%94%E5%BA%8A%E7%8E%87%E6%9F%A5%E8%A9%A2.aspx?ihospital=10&ffloor=" response_chimei = requests.get(url_chimei) response_chimei.encoding = 'utf-8' soup_chimei = BeautifulSoup(response_chimei.text, 'html.parser') table_chimei = soup_chimei.find('table', {'id': 'DG1'}) data_chimei = [] rows_chimei = table_chimei.find_all('tr') headers_chimei = [th.text.strip() for th in rows_chimei[0].find_all('td')] for row in rows_chimei[1:]: cols = row.find_all('td') cols = [col.text.strip() for col in cols] data_chimei.append(cols) df_chimei = pd.DataFrame(data_chimei, columns=headers_chimei) # 更改奇美醫院的列名 df_chimei = df_chimei.rename(columns={ '總床數': '病床數' }) df_chimei['醫院'] = '奇美醫院' return df_chimei # 定義函數來獲取並處理成大醫院的數據 def get_ncku_data(): url_ncku = "https://web.hosp.ncku.edu.tw/nckm/Bedstatus/BedStatus.aspx" response_ncku = requests.get(url_ncku) response_ncku.encoding = 'utf-8' soup_ncku = BeautifulSoup(response_ncku.text, 'html.parser') table_ncku = soup_ncku.find('table', {'id': 'GV_EmgInsure'}) data_ncku = [] rows_ncku = table_ncku.find_all('tr') headers_ncku = [th.text.strip() for th in rows_ncku[0].find_all('th')] for row in rows_ncku[1:]: cols = row.find_all('td') cols = [col.text.strip() for col in cols] data_ncku.append(cols) df_ncku = pd.DataFrame(data_ncku, columns=headers_ncku) df_ncku['醫院'] = '成大醫院' # 計算佔床率並添加到成大醫院的 DataFrame df_ncku['佔床率'] = (df_ncku['佔床數'].astype(int) / df_ncku['病床數'].astype(int) * 100).round(2).astype(str) + '%' # 將佔床率移動到倒數第二欄 cols = df_ncku.columns.tolist() cols.insert(-1, cols.pop(cols.index('佔床率'))) df_ncku = df_ncku[cols] return df_ncku # 主程序 st.title("醫院病床狀況") # 多項選擇下拉式選單,預設選項為"請選擇" options = st.multiselect( "選擇要查看的醫院", ["台南市立醫院", "奇美醫院", "成大醫院"], default=[] ) # 查詢按鈕 if st.button("查詢"): if not options: st.warning("請選擇至少一個醫院進行查詢。") else: selected_data = [] if "台南市立醫院" in options: df_tmh = get_tmh_data() selected_data.append(df_tmh) if "奇美醫院" in options: df_chimei = get_chimei_data() selected_data.append(df_chimei) if "成大醫院" in options: df_ncku = get_ncku_data() selected_data.append(df_ncku) if selected_data: df_combined = pd.concat(selected_data, ignore_index=True) st.write(df_combined) # 美化病床類別分布直條圖 fig_bar = px.bar(df_combined, x='病床類別', y='病床數', color='醫院', title='病床類別分布', text='病床數', barmode='group', color_discrete_sequence=px.colors.qualitative.Pastel) fig_bar.update_traces(texttemplate='%{text:.2s}', textposition='outside') fig_bar.update_layout(uniformtext_minsize=8, uniformtext_mode='hide', xaxis_title='病床類別', yaxis_title='病床數', title_x=0.5, template='plotly_white') st.plotly_chart(fig_bar) # 病床類別分布圓餅圖 fig_pie_beds = px.pie(df_combined, names='病床類別', values='病床數', title='病床類別分布', color_discrete_sequence=px.colors.qualitative.Pastel) st.plotly_chart(fig_pie_beds) # 空床數分布圓餅圖 fig_pie_empty = px.pie(df_combined, names='病床類別', values='空床數', title='空床數分布', color_discrete_sequence=px.colors.qualitative.Pastel) st.plotly_chart(fig_pie_empty)