Rozeeeee commited on
Commit
3c7978a
·
verified ·
1 Parent(s): 5e53466

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -1
app.py CHANGED
@@ -1,4 +1,43 @@
1
- remarks = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  for i in range(6):
3
  remark = soup.find('span', id=f'Lbl備註{i}')
4
  if remark:
 
1
+ irt requests
2
+ from bs4 import BeautifulSoup
3
+ import pandas as pd
4
+ import gradio as grmpo
5
+
6
+ def get_hospital_data(url):
7
+ # 發送GET請求獲取網頁內容
8
+ response = requests.get(url)
9
+ response.encoding = 'utf-8' # 設置正確的編碼
10
+
11
+ # 使用BeautifulSoup解析HTML
12
+ soup = BeautifulSoup(response.text, 'html.parser')
13
+
14
+ # 提取醫院名稱
15
+ hospital_name = soup.find('span', id='Lbl抬頭').text.strip()
16
+
17
+ # 提取查詢院區
18
+ queried_hospital = soup.find('span', id='Lbl結果').text.strip().split(':')[1].split()[0]
19
+
20
+ # 提取病床數據
21
+ table = soup.find('table', id='DG1')
22
+ rows = table.find_all('tr')[1:] # 跳過表頭
23
+
24
+ # 解析病床數據
25
+ bed_data = []
26
+ for row in rows:
27
+ cols = row.find_all('td')
28
+ if len(cols) == 5:
29
+ category = cols[0].text.strip()
30
+ total = int(cols[1].text.strip())
31
+ occupied = int(cols[2].text.strip())
32
+ available = int(cols[3].text.strip())
33
+ rate = float(cols[4].text.strip().rstrip('%'))
34
+ bed_data.append([category, total, occupied, available, rate])
35
+
36
+ # 創建DataFrame
37
+ df = pd.DataFrame(bed_data, columns=['病床類別', '總床數', '佔床數', '空床數', '佔床率'])
38
+
39
+ # 提取備註
40
+ remarks = []
41
  for i in range(6):
42
  remark = soup.find('span', id=f'Lbl備註{i}')
43
  if remark: