Rooobert commited on
Commit
083a08a
·
verified ·
1 Parent(s): ec055df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -8
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # file_path: app.py
2
  import streamlit as st
3
  import requests
4
  from bs4 import BeautifulSoup
@@ -140,12 +139,13 @@ def upload_to_google_sheets(df):
140
  worksheet.update([df1.columns.values.tolist()] + df1.values.tolist())
141
  return "資料已成功上傳到 Google Sheet!"
142
 
 
143
  if mode == "資料爬取":
144
  st.header("爬取台南飯店資料")
145
  if st.button("開始爬取"):
146
  df = scrape_booking_hotel()
147
  if not df.empty:
148
- st.dataframe(df) # 顯示數據
149
  df.to_csv('booking_hotels_tainan.csv', index=False, encoding='utf-8-sig')
150
  st.success("資料爬取成功,已儲存至 booking_hotels_tainan.csv")
151
  else:
@@ -155,15 +155,13 @@ elif mode == "資料視覺化":
155
  st.header("分析與視覺化")
156
  try:
157
  df = pd.read_csv('booking_hotels_tainan.csv', encoding='utf-8-sig')
158
- df['價格'] = pd.to_numeric(df['價格'], errors='coerce') # 無效值轉為 NaN
159
- df['評分'] = df['評分'].apply(clean_rating) # 清理評分
160
- df = df.dropna(subset=['價格']) # 移除價格為 NaN 的行
161
 
162
- # 顯示數據摘要
163
  st.write(f"有效數據行數:{len(df)}")
164
  st.write(f"價格缺失值數量:{df['價格'].isna().sum()}")
165
 
166
- # 繪製圖表
167
  scatter_fig = create_price_rating_scatter(df)
168
  if scatter_fig:
169
  st.plotly_chart(scatter_fig)
@@ -180,4 +178,4 @@ elif mode == "上傳至 Google Sheet":
180
  result = upload_to_google_sheets(df)
181
  st.success(result)
182
  except Exception as e:
183
- st.error(f"上傳資料時發生錯誤:{e}")
 
 
1
  import streamlit as st
2
  import requests
3
  from bs4 import BeautifulSoup
 
139
  worksheet.update([df1.columns.values.tolist()] + df1.values.tolist())
140
  return "資料已成功上傳到 Google Sheet!"
141
 
142
+ # Streamlit app implementation
143
  if mode == "資料爬取":
144
  st.header("爬取台南飯店資料")
145
  if st.button("開始爬取"):
146
  df = scrape_booking_hotel()
147
  if not df.empty:
148
+ st.dataframe(df)
149
  df.to_csv('booking_hotels_tainan.csv', index=False, encoding='utf-8-sig')
150
  st.success("資料爬取成功,已儲存至 booking_hotels_tainan.csv")
151
  else:
 
155
  st.header("分析與視覺化")
156
  try:
157
  df = pd.read_csv('booking_hotels_tainan.csv', encoding='utf-8-sig')
158
+ df['價格'] = pd.to_numeric(df['價格'], errors='coerce')
159
+ df['評分'] = df['評分'].apply(clean_rating)
160
+ df = df.dropna(subset=['價格'])
161
 
 
162
  st.write(f"有效數據行數:{len(df)}")
163
  st.write(f"價格缺失值數量:{df['價格'].isna().sum()}")
164
 
 
165
  scatter_fig = create_price_rating_scatter(df)
166
  if scatter_fig:
167
  st.plotly_chart(scatter_fig)
 
178
  result = upload_to_google_sheets(df)
179
  st.success(result)
180
  except Exception as e:
181
+ st.error(f"上傳資料時發生錯誤:{e}")