Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import requests
|
4 |
+
from bs4 import BeautifulSoup
|
5 |
+
from google.oauth2.service_account import Credentials
|
6 |
+
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)
|
16 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
17 |
+
title = soup.find("h2", class_="d2fee87262 pp-header__title").text.strip()
|
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:
|
27 |
+
scope = ['https://www.googleapis.com/auth/spreadsheets']
|
28 |
+
creds = Credentials.from_service_account_file(creds_file, scopes=scope)
|
29 |
+
gs = gspread.authorize(creds)
|
30 |
+
sheet = gs.open_by_url(spreadsheet_url)
|
31 |
+
worksheet = sheet.get_worksheet(0)
|
32 |
+
worksheet.clear()
|
33 |
+
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
|
34 |
+
st.success("Data uploaded to Google Sheets successfully!")
|
35 |
+
except Exception as e:
|
36 |
+
st.error(f"Error uploading data: {e}")
|
37 |
+
|
38 |
+
# Step 3: 主程式 - 使用 Streamlit UI 設定參數
|
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 |
+
headers = {
|
44 |
+
"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"
|
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("CSV 檔案需包含 'Date' 和 'Value' 欄位")
|
66 |
+
|
67 |
+
# Step 5: 下載字體檔案(可選)
|
68 |
+
font_url = st.text_input("字體檔案 URL", "https://example.com/TaipeiSansTCBeta-Regular.ttf")
|
69 |
+
font_path = "TaipeiSansTCBeta-Regular.ttf"
|
70 |
+
|
71 |
+
if st.button("下載字體檔案"):
|
72 |
+
try:
|
73 |
+
response = requests.get(font_url)
|
74 |
+
with open(font_path, "wb") as f:
|
75 |
+
f.write(response.content)
|
76 |
+
st.success("字體下載成功!")
|
77 |
+
except Exception as e:
|
78 |
+
st.error(f"字體下載失敗:{e}")
|