Rozeeeee commited on
Commit
4f50550
·
verified ·
1 Parent(s): 71f577b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -124
app.py CHANGED
@@ -1,128 +1,61 @@
 
1
  import requests
2
  from bs4 import BeautifulSoup
3
  import pandas as pd
4
  import plotly.graph_objects as go
5
- import streamlit as st
6
-
7
- # 設定應用標題
8
- st.title("餐廳資料抓取與分析")
9
-
10
- # 添加 CSS 樣式
11
- st.markdown(
12
- """
13
- <style>
14
- /* 全局樣式 */
15
- body {
16
- background-color: #f5f5f5;
17
- color: #333;
18
- font-family: 'Arial', sans-serif;
19
- }
20
-
21
- /* 標題樣式 */
22
- .stApp h1 {
23
- color: #2c3e50;
24
- text-align: center;
25
- font-size: 3em;
26
- margin-top: 20px;
27
- margin-bottom: 20px;
28
- }
29
-
30
- /* 表格樣式 */
31
- .stApp .stDataFrame {
32
- border: 2px solid #2c3e50;
33
- border-radius: 10px;
34
- margin-top: 20px;
35
- }
36
-
37
- /* 按鈕樣式 */
38
- .stButton button {
39
- background-color: #3498db;
40
- color: white;
41
- border: none;
42
- border-radius: 5px;
43
- padding: 10px 20px;
44
- font-size: 16px;
45
- margin-top: 20px;
46
- cursor: pointer;
47
- }
48
-
49
- .stButton button:hover {
50
- background-color: #2980b9;
51
- }
52
-
53
- /* 圖表標題樣式 */
54
- .stApp .stPlotlyChart {
55
- margin-top: 30px;
56
- }
57
- </style>
58
- """,
59
- unsafe_allow_html=True
60
- )
61
-
62
- # 從 Google 試算表中讀取 URLs
63
- sheet_id = "1W20lawjiQtEpljUKoEaMVPDlSdnhvJLPUy2jk5xao_w"
64
- urls_df = pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv")
65
-
66
- # 將 URLs 轉換為列表
67
- urls = urls_df['網址'].tolist()
68
-
69
- # 初始化一個空的 DataFrame 列表來儲存所有資料
70
- df_list = []
71
-
72
- # 迭代每個網址並抓取資料
73
- for url in urls:
74
- response = requests.get(url)
75
- soup = BeautifulSoup(response.content, 'html.parser')
76
-
77
- # 解析並抓取所需資料
78
- title_tag = soup.find('h1', class_='restaurant-details__heading--title')
79
- title = title_tag.text.strip() if title_tag else 'N/A'
80
-
81
- address_tag = soup.find('li', class_='restaurant-details__heading--address')
82
- address = address_tag.text.strip() if address_tag else 'N/A'
83
-
84
- phone_tag = soup.find('a', {'data-event': 'CTA_tel'})
85
- phone = phone_tag['href'].replace('tel:', '') if phone_tag else 'N/A'
86
-
87
- description_tag = soup.find('div', class_='restaurant-details__description--text')
88
- description = description_tag.text.strip() if description_tag else 'N/A'
89
-
90
- # NOTE: Assuming latitude and longitude are not available from the current page content, you can omit them or fetch them if necessary
91
- lat = 'N/A'
92
- lon = 'N/A'
93
-
94
- # 將抓取的資料新增到列表中
95
- df_list.append({
96
- 'Title': title,
97
- 'Address': address,
98
- 'Phone': phone,
99
- 'Description': description,
100
- 'Latitude': lat,
101
- 'Longitude': lon
102
- })
103
-
104
- # 使用 pd.DataFrame() 將所有資料合併成一個 DataFrame
105
- df = pd.DataFrame(df_list)
106
-
107
- # 顯示抓取的資料
108
- st.subheader("抓取的餐廳資料")
109
- st.dataframe(df)
110
-
111
- # 統計每個區的商家數量
112
- df['Area'] = df['Address'].str.extract(r'(\w+區)') # 提取區域
113
- area_counts = df['Area'].value_counts() # 統計各區的商家數量
114
-
115
- # 繪製柱狀圖
116
- fig_bar = go.Figure(data=[go.Bar(x=area_counts.index, y=area_counts.values)])
117
- fig_bar.update_layout(title='每個區的商家數量', xaxis_title='區域', yaxis_title='商家數量')
118
-
119
- # 顯示柱狀圖
120
- st.plotly_chart(fig_bar)
121
-
122
- # 繪製圓餅圖
123
- fig_pie = go.Figure(data=[go.Pie(labels=area_counts.index, values=area_counts.values)])
124
- fig_pie.update_layout(title='每個區的商家數量比例')
125
-
126
- # 按鈕來顯示圓餅圖
127
- if st.button('顯示每個區的商家數量比例圓餅圖'):
128
- st.plotly_chart(fig_pie)
 
1
+ from flask import Flask, render_template, request
2
  import requests
3
  from bs4 import BeautifulSoup
4
  import pandas as pd
5
  import plotly.graph_objects as go
6
+ import plotly.io as pio
7
+
8
+ app = Flask(__name__)
9
+
10
+ @app.route('/')
11
+ def home():
12
+ # 設定應用標題
13
+ title = "餐廳資料抓取與分析"
14
+
15
+ # Google 試算表中讀取 URLs
16
+ sheet_id = "1W20lawjiQtEpljUKoEaMVPDlSdnhvJLPUy2jk5xao_w"
17
+ urls_df = pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv")
18
+
19
+ # URLs 轉換為列表
20
+ urls = urls_df['網址'].tolist()
21
+
22
+ # 初始化一個空的 DataFrame 列表來儲存所有資料
23
+ df_list = []
24
+
25
+ # 迭代每個網址並抓取資料
26
+ for url in urls:
27
+ response = requests.get(url)
28
+ soup = BeautifulSoup(response.content, 'html.parser')
29
+
30
+ # 解析並抓取所需資料
31
+ title_tag = soup.find('h1', class_='restaurant-details__heading--title')
32
+ title = title_tag.text.strip() if title_tag else 'N/A'
33
+
34
+ address_tag = soup.find('li', class_='restaurant-details__heading--address')
35
+ address = address_tag.text.strip() if address_tag else 'N/A'
36
+
37
+ phone_tag = soup.find('a', {'data-event': 'CTA_tel'})
38
+ phone = phone_tag['href'].replace('tel:', '') if phone_tag else 'N/A'
39
+
40
+ description_tag = soup.find('div', class_='restaurant-details__description--text')
41
+ description = description_tag.text.strip() if description_tag else 'N/A'
42
+
43
+ # NOTE: Assuming latitude and longitude are not available from the current page content, you can omit them or fetch them if necessary
44
+ lat = 'N/A'
45
+ lon = 'N/A'
46
+
47
+ # 將抓取的資料新增到列表中
48
+ df_list.append({
49
+ 'Title': title,
50
+ 'Address': address,
51
+ 'Phone': phone,
52
+ 'Description': description,
53
+ 'Latitude': lat,
54
+ 'Longitude': lon
55
+ })
56
+
57
+ # 使用 pd.DataFrame() 將所有資料合併成一個 DataFrame
58
+ df = pd.DataFrame(df_list)
59
+
60
+ # 統計每個區的商家數量
61
+ df['Area'] = df['