Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
""
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
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['
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|