Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ import json # 用於解析 JSON 文件
|
|
10 |
# Streamlit UI 標題
|
11 |
st.title("Booking.com 多項目數據抓取和視覺化展示")
|
12 |
|
13 |
-
# 定義 URL
|
14 |
urls = [
|
15 |
"https://www.booking.com/hotel/it/appartamento-via-genova-roma.zh-tw.html",
|
16 |
"https://www.booking.com/hotel/it/giulia-39-s-coliseum.zh-tw.html",
|
@@ -25,7 +25,18 @@ urls = [
|
|
25 |
"https://www.booking.com/hotel/us/loews-royal-pacific-resort-at-universal.zh-tw.html",
|
26 |
"https://www.booking.com/hotel/us/buena-vista-suites.zh-tw.html",
|
27 |
"https://www.booking.com/hotel/us/ramada-international-drive.zh-tw.html",
|
28 |
-
"https://www.booking.com/hotel/us/lake-buena-vista-14651-chelonia-parkway.zh-tw.html"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
]
|
30 |
|
31 |
# 設定 User-Agent
|
@@ -45,7 +56,7 @@ def fetch_booking_data(url, headers):
|
|
45 |
st.error(f"Error fetching data from {url}: {e}")
|
46 |
return None
|
47 |
|
48 |
-
#
|
49 |
st.header("抓取數據")
|
50 |
data = []
|
51 |
for url in urls:
|
@@ -60,7 +71,7 @@ if data:
|
|
60 |
else:
|
61 |
st.warning("無法從任何 URL 抓取數據。")
|
62 |
|
63 |
-
#
|
64 |
def upload_to_google_sheet(df, spreadsheet_url, creds_file_content):
|
65 |
try:
|
66 |
# 將金鑰內容轉換為字典
|
@@ -79,7 +90,7 @@ def upload_to_google_sheet(df, spreadsheet_url, creds_file_content):
|
|
79 |
except Exception as e:
|
80 |
st.error(f"數據上傳失敗:{e}")
|
81 |
|
82 |
-
# Google Sheets
|
83 |
st.header("上傳到 Google Sheets")
|
84 |
spreadsheet_url = st.text_input("Google Sheets URL", "https://docs.google.com/spreadsheets/d/1iOzoii9bVAmqlcqnseoqjZBkBuaFcpbIvUxZeRJ2kmk/edit?gid=0#gid=0")
|
85 |
creds_file = st.file_uploader("上傳 Google API 金鑰檔案", type=["json"])
|
@@ -91,7 +102,7 @@ if st.button("上傳數據至 Google Sheets") and data:
|
|
91 |
else:
|
92 |
st.error("請上傳 Google API 金鑰檔案")
|
93 |
|
94 |
-
#
|
95 |
st.header("數據視覺化")
|
96 |
if not df.empty:
|
97 |
# 確保評分可以轉換為數字
|
|
|
10 |
# Streamlit UI 標題
|
11 |
st.title("Booking.com 多項目數據抓取和視覺化展示")
|
12 |
|
13 |
+
# 定義 URL 列表(包含所有提供的 URL)
|
14 |
urls = [
|
15 |
"https://www.booking.com/hotel/it/appartamento-via-genova-roma.zh-tw.html",
|
16 |
"https://www.booking.com/hotel/it/giulia-39-s-coliseum.zh-tw.html",
|
|
|
25 |
"https://www.booking.com/hotel/us/loews-royal-pacific-resort-at-universal.zh-tw.html",
|
26 |
"https://www.booking.com/hotel/us/buena-vista-suites.zh-tw.html",
|
27 |
"https://www.booking.com/hotel/us/ramada-international-drive.zh-tw.html",
|
28 |
+
"https://www.booking.com/hotel/us/lake-buena-vista-14651-chelonia-parkway.zh-tw.html",
|
29 |
+
"https://www.booking.com/hotel/it/trevi-apartment-roma1.zh-tw.html",
|
30 |
+
"https://www.booking.com/hotel/it/white-flat-colosseo.zh-tw.html",
|
31 |
+
"https://www.booking.com/hotel/it/via-cavour-238.zh-tw.html",
|
32 |
+
"https://www.booking.com/hotel/it/modern-apartment-near-the-vatican-roma.zh-tw.html",
|
33 |
+
"https://www.booking.com/hotel/it/apt-prati-lt-vatican-and-center.zh-tw.html",
|
34 |
+
"https://www.booking.com/hotel/it/lion-99.zh-tw.html",
|
35 |
+
"https://www.booking.com/hotel/it/sweet-dream-cavour-roma1.zh-tw.html",
|
36 |
+
"https://www.booking.com/hotel/it/cavour-1.zh-tw.html",
|
37 |
+
"https://www.booking.com/hotel/it/eufonia-vinyl-apartment.zh-tw.html",
|
38 |
+
"https://www.booking.com/hotel/it/panisperna-apartment.zh-tw.html",
|
39 |
+
"https://www.booking.com/hotel/it/navona-panoramic-penthouse.zh-tw.html"
|
40 |
]
|
41 |
|
42 |
# 設定 User-Agent
|
|
|
56 |
st.error(f"Error fetching data from {url}: {e}")
|
57 |
return None
|
58 |
|
59 |
+
# 抓取數據並顯示
|
60 |
st.header("抓取數據")
|
61 |
data = []
|
62 |
for url in urls:
|
|
|
71 |
else:
|
72 |
st.warning("無法從任何 URL 抓取數據。")
|
73 |
|
74 |
+
# 定義上傳到 Google Sheets 的函數
|
75 |
def upload_to_google_sheet(df, spreadsheet_url, creds_file_content):
|
76 |
try:
|
77 |
# 將金鑰內容轉換為字典
|
|
|
90 |
except Exception as e:
|
91 |
st.error(f"數據上傳失敗:{e}")
|
92 |
|
93 |
+
# 上傳到 Google Sheets
|
94 |
st.header("上傳到 Google Sheets")
|
95 |
spreadsheet_url = st.text_input("Google Sheets URL", "https://docs.google.com/spreadsheets/d/1iOzoii9bVAmqlcqnseoqjZBkBuaFcpbIvUxZeRJ2kmk/edit?gid=0#gid=0")
|
96 |
creds_file = st.file_uploader("上傳 Google API 金鑰檔案", type=["json"])
|
|
|
102 |
else:
|
103 |
st.error("請上傳 Google API 金鑰檔案")
|
104 |
|
105 |
+
# 數據視覺化
|
106 |
st.header("數據視覺化")
|
107 |
if not df.empty:
|
108 |
# 確保評分可以轉換為數字
|