ttttttttt / app.py
Rozeeeee's picture
Update app.py
3c7978a verified
irt requests
from bs4 import BeautifulSoup
import pandas as pd
import gradio as grmpo
def get_hospital_data(url):
# 發送GET請求獲取網頁內容
response = requests.get(url)
response.encoding = 'utf-8' # 設置正確的編碼
# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
# 提取醫院名稱
hospital_name = soup.find('span', id='Lbl抬頭').text.strip()
# 提取查詢院區
queried_hospital = soup.find('span', id='Lbl結果').text.strip().split(':')[1].split()[0]
# 提取病床數據
table = soup.find('table', id='DG1')
rows = table.find_all('tr')[1:] # 跳過表頭
# 解析病床數據
bed_data = []
for row in rows:
cols = row.find_all('td')
if len(cols) == 5:
category = cols[0].text.strip()
total = int(cols[1].text.strip())
occupied = int(cols[2].text.strip())
available = int(cols[3].text.strip())
rate = float(cols[4].text.strip().rstrip('%'))
bed_data.append([category, total, occupied, available, rate])
# 創建DataFrame
df = pd.DataFrame(bed_data, columns=['病床類別', '總床數', '佔床數', '空床數', '佔床率'])
# 提取備註
remarks = []
for i in range(6):
remark = soup.find('span', id=f'Lbl備註{i}')
if remark:
remarks.append(remark.text.strip())
# 格式化輸出
result = f"{hospital_name}\n查詢院區: {queried_hospital}\n\n各類病床明細表:\n{df.to_string(index=False)}\n\n備註:\n" + "\n".join(remarks)
return result
def gradio_interface(url):
return get_hospital_data(url)
iface = gr.Interface(fn=gradio_interface, inputs="text", outputs="text", title="Hospital Bed Data")
iface.launch()