Update app.py
Browse files
app.py
CHANGED
@@ -7,9 +7,28 @@ import gspread
|
|
7 |
import plotly.express as px
|
8 |
|
9 |
# Streamlit UI 標題
|
10 |
-
st.title("Booking.com
|
11 |
|
12 |
-
# Step 1: 抓取 Booking.com 數據
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def fetch_booking_data(url, headers):
|
14 |
try:
|
15 |
response = requests.get(url, headers=headers)
|
@@ -18,9 +37,23 @@ def fetch_booking_data(url, headers):
|
|
18 |
rating = soup.find("div", class_="bcdcb105b3 f45d8e4c32 df64fda51b").text.strip()
|
19 |
return {"標題": title, "評分": rating}
|
20 |
except Exception as e:
|
21 |
-
st.error(f"Error fetching data: {e}")
|
22 |
return None
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
# Step 2: 上傳數據至 Google Sheets
|
25 |
def upload_to_google_sheet(df, spreadsheet_url, creds_file):
|
26 |
try:
|
@@ -35,44 +68,25 @@ def upload_to_google_sheet(df, spreadsheet_url, creds_file):
|
|
35 |
except Exception as e:
|
36 |
st.error(f"Error uploading data: {e}")
|
37 |
|
38 |
-
#
|
39 |
-
url = st.text_input("Booking.com URL", "https://www.booking.com/hotel/it/appartamento-via-genova-roma.zh-tw.html")
|
40 |
spreadsheet_url = st.text_input("Google Sheets URL", "https://docs.google.com/spreadsheets/d/1iOzoii9bVAmqlcqnseoqjZBkBuaFcpbIvUxZeRJ2kmk/edit?gid=0#gid=0")
|
41 |
creds_file = st.file_uploader("上傳 Google API 金鑰檔案", type=["json"])
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
if st.button("抓取數據並上傳至 Google Sheets"):
|
48 |
-
data = fetch_booking_data(url, headers)
|
49 |
-
if data:
|
50 |
-
df = pd.DataFrame([data])
|
51 |
-
st.write("抓取到的數據:", df)
|
52 |
-
if creds_file is not None:
|
53 |
-
upload_to_google_sheet(df, spreadsheet_url, creds_file.name)
|
54 |
-
|
55 |
-
# Step 4: 顯示動態數據圖表
|
56 |
-
uploaded_file = st.file_uploader("上傳 CSV 數據檔案以生成圖表", type=["csv"])
|
57 |
-
if uploaded_file is not None:
|
58 |
-
df = pd.read_csv(uploaded_file)
|
59 |
-
st.write("上傳的數據:", df)
|
60 |
-
|
61 |
-
if "Date" in df.columns and "Value" in df.columns:
|
62 |
-
fig = px.line(df, x='Date', y='Value', title="動態數據圖")
|
63 |
-
st.plotly_chart(fig)
|
64 |
else:
|
65 |
-
st.error("
|
66 |
|
67 |
-
# Step
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
st.error(f"字體下載失敗:{e}")
|
|
|
7 |
import plotly.express as px
|
8 |
|
9 |
# Streamlit UI 標題
|
10 |
+
st.title("Booking.com 多項目數據抓取和視覺化展示")
|
11 |
|
12 |
+
# Step 1: 多 URL 抓取 Booking.com 數據
|
13 |
+
urls = [
|
14 |
+
"https://www.booking.com/hotel/it/appartamento-via-genova-roma.zh-tw.html",
|
15 |
+
"https://www.booking.com/hotel/it/giulia-39-s-coliseum.zh-tw.html",
|
16 |
+
"https://www.booking.com/hotel/it/le-stelle-dell-esquilino.zh-tw.html",
|
17 |
+
"https://www.booking.com/hotel/it/retro-flat-termini.zh-tw.html",
|
18 |
+
"https://www.booking.com/hotel/it/domus-fanti.zh-tw.html",
|
19 |
+
"https://www.booking.com/hotel/it/radiant-retreat-in-esquilino.zh-tw.html",
|
20 |
+
"https://www.booking.com/hotel/it/over-the-roof-top-roma.zh-tw.html",
|
21 |
+
"https://www.booking.com/hotel/it/rome-sweet-home-roma1234.zh-tw.html",
|
22 |
+
"https://www.booking.com/hotel/it/visione-guest-house-via-delle-fratte-36.zh-tw.html",
|
23 |
+
"https://www.booking.com/hotel/it/appartamento-roma-centro-roma5.zh-tw.html"
|
24 |
+
]
|
25 |
+
|
26 |
+
# 設定 User-Agent
|
27 |
+
headers = {
|
28 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
|
29 |
+
}
|
30 |
+
|
31 |
+
# 抓取每個 URL 的標題和評分
|
32 |
def fetch_booking_data(url, headers):
|
33 |
try:
|
34 |
response = requests.get(url, headers=headers)
|
|
|
37 |
rating = soup.find("div", class_="bcdcb105b3 f45d8e4c32 df64fda51b").text.strip()
|
38 |
return {"標題": title, "評分": rating}
|
39 |
except Exception as e:
|
40 |
+
st.error(f"Error fetching data from {url}: {e}")
|
41 |
return None
|
42 |
|
43 |
+
# 逐一處理 URL 並收集數據
|
44 |
+
data = []
|
45 |
+
for url in urls:
|
46 |
+
result = fetch_booking_data(url, headers)
|
47 |
+
if result:
|
48 |
+
data.append(result)
|
49 |
+
|
50 |
+
# 將數據轉換為 DataFrame
|
51 |
+
if data:
|
52 |
+
df = pd.DataFrame(data)
|
53 |
+
st.write("抓取到的數據:", df)
|
54 |
+
else:
|
55 |
+
st.warning("無法從任何 URL 抓取數據。")
|
56 |
+
|
57 |
# Step 2: 上傳數據至 Google Sheets
|
58 |
def upload_to_google_sheet(df, spreadsheet_url, creds_file):
|
59 |
try:
|
|
|
68 |
except Exception as e:
|
69 |
st.error(f"Error uploading data: {e}")
|
70 |
|
71 |
+
# Google Sheets URL 和金鑰文件上傳
|
|
|
72 |
spreadsheet_url = st.text_input("Google Sheets URL", "https://docs.google.com/spreadsheets/d/1iOzoii9bVAmqlcqnseoqjZBkBuaFcpbIvUxZeRJ2kmk/edit?gid=0#gid=0")
|
73 |
creds_file = st.file_uploader("上傳 Google API 金鑰檔案", type=["json"])
|
74 |
|
75 |
+
# 上傳至 Google Sheets
|
76 |
+
if st.button("上傳數據至 Google Sheets") and data:
|
77 |
+
if creds_file is not None:
|
78 |
+
upload_to_google_sheet(df, spreadsheet_url, creds_file.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
else:
|
80 |
+
st.error("請上傳 Google API 金鑰檔案")
|
81 |
|
82 |
+
# Step 3: 顯示抓取的數據為動態圖表
|
83 |
+
if not df.empty:
|
84 |
+
# 確保評分可以轉換為數字
|
85 |
+
df['評分'] = pd.to_numeric(df['評分'], errors='coerce')
|
86 |
+
|
87 |
+
# 使用 Plotly 繪製條形圖
|
88 |
+
fig = px.bar(df, x='標題', y='評分', title="Booking.com 評分比較", labels={'標題': '酒店標題', '評分': '評分'})
|
89 |
+
fig.update_layout(xaxis_title="酒店標題", yaxis_title="評分", xaxis_tickangle=-45)
|
90 |
+
st.plotly_chart(fig)
|
91 |
+
else:
|
92 |
+
st.warning("無可用數據繪製圖表")
|
|