| import requests |
| from bs4 import BeautifulSoup |
| import pandas as pd |
| import gradio as gr |
|
|
| def get_hospital_data(url): |
| |
| response = requests.get(url) |
| response.encoding = 'utf-8' |
|
|
| |
| 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]) |
|
|
| |
| 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() |
|
|