File size: 1,808 Bytes
77773ca
448066b
 
77773ca
a33f47f
448066b
 
77773ca
448066b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
from bs4 import BeautifulSoup
import pandas as pd
import gradio as gr

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()