Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,6 @@ urls = urls_df['網址'].tolist()
|
|
17 |
# 初始化一個空的 DataFrame 列表來儲存所有資料
|
18 |
df_list = []
|
19 |
|
20 |
-
|
21 |
# 迭代每個網址並抓取資料
|
22 |
for url in urls:
|
23 |
response = requests.get(url)
|
@@ -36,91 +35,12 @@ for url in urls:
|
|
36 |
description_tag = soup.find('div', class_='restaurant-details__description--text')
|
37 |
description = description_tag.text.strip() if description_tag else 'N/A'
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
# 將抓取的資料新增到列表中
|
42 |
-
df_list.append({
|
43 |
-
'Title': title,
|
44 |
-
'Address': address,
|
45 |
-
'Phone': phone,
|
46 |
-
'Description': description,
|
47 |
-
'Latitude': lat,
|
48 |
-
'Longitude': lon
|
49 |
-
})
|
50 |
-
|
51 |
-
# 使用 pd.DataFrame() 將所有資料合併成一個 DataFrame
|
52 |
-
df = pd.DataFrame(df_list)
|
53 |
-
|
54 |
-
# 顯示抓取的資料
|
55 |
-
st.subheader("抓取的餐廳資料")
|
56 |
-
st.dataframe(df)
|
57 |
-
|
58 |
-
# 統計每個區的商家數量
|
59 |
-
df['Area'] = df['Address'].str.extract(r'(\w+區)') # 提取區域
|
60 |
-
area_counts = df['Area'].value_counts() # 統計各區的商家數量
|
61 |
-
|
62 |
-
# 繪製柱狀圖
|
63 |
-
fig_bar = go.Figure(data=[go.Bar(x=area_counts.index, y=area_counts.values)])
|
64 |
-
fig_bar.update_layout(title='每個區的商家數量', xaxis_title='區域', yaxis_title='商家數量')
|
65 |
-
|
66 |
-
# 顯示柱狀圖
|
67 |
-
st.plotly_chart(fig_bar)
|
68 |
-
|
69 |
-
# 繪製圓餅圖
|
70 |
-
fig_pie = go.Figure(data=[go.Pie(labels=area_counts.index, values=area_counts.values)])
|
71 |
-
fig_pie.update_layout(title='每個區的商家數量比例')
|
72 |
-
|
73 |
-
# 按鈕來顯示圓餅圖
|
74 |
-
if st.button('顯示每個區的商家數量比例圓餅圖'):
|
75 |
-
st.plotly_chart(fig_pie)NameError: name 'lat' is not defined
|
76 |
-
mport requests
|
77 |
-
from bs4 import BeautifulSoup
|
78 |
-
import pandas as pd
|
79 |
-
import plotly.graph_objects as go
|
80 |
-
import streamlit as st
|
81 |
-
|
82 |
-
# 設定應用標題
|
83 |
-
st.title("餐廳資料抓取與分析")
|
84 |
-
|
85 |
-
# 從 Google 試算表中讀取 URLs
|
86 |
-
sheet_id = "1W20lawjiQtEpljUKoEaMVPDlSdnhvJLPUy2jk5xao_w"
|
87 |
-
urls_df = pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv")
|
88 |
-
|
89 |
-
# 將 URLs 轉換為列表
|
90 |
-
urls = urls_df['網址'].tolist()
|
91 |
-
|
92 |
-
# 初始化一個空的 DataFrame 列表來儲存所有資料
|
93 |
-
df_list = []
|
94 |
-
|
95 |
-
|
96 |
-
# 迭代每個網址並抓取資料
|
97 |
-
for url in urls:
|
98 |
-
response = requests.get(url)
|
99 |
-
soup = BeautifulSoup(response.content, 'html.parser')
|
100 |
-
|
101 |
-
# 解析並抓取所需資料
|
102 |
-
title_tag = soup.find('h1', class_='restaurant-details__heading--title')
|
103 |
-
title = title_tag.text.strip() if title_tag else 'N/A'
|
104 |
-
|
105 |
-
address_tag = soup.find('li', class_='restaurant-details__heading--address')
|
106 |
-
address = address_tag.text.strip() if address_tag else 'N/A'
|
107 |
-
|
108 |
-
phone_tag = soup.find('a', {'data-event': 'CTA_tel'})
|
109 |
-
phone = phone_tag['href'].replace('tel:', '') if phone_tag else 'N/A'
|
110 |
-
|
111 |
-
description_tag = soup.find('div', class_='restaurant-details__description--text')
|
112 |
-
description = description_tag.text.strip() if description_tag else 'N/A'
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
# 將抓取的資料新增到列表中
|
117 |
df_list.append({
|
118 |
'Title': title,
|
119 |
'Address': address,
|
120 |
'Phone': phone,
|
121 |
-
'Description': description
|
122 |
-
'Latitude': lat,
|
123 |
-
'Longitude': lon
|
124 |
})
|
125 |
|
126 |
# 使用 pd.DataFrame() 將所有資料合併成一個 DataFrame
|
@@ -147,4 +67,4 @@ fig_pie.update_layout(title='每個區的商家數量比例')
|
|
147 |
|
148 |
# 按鈕來顯示圓餅圖
|
149 |
if st.button('顯示每個區的商家數量比例圓餅圖'):
|
150 |
-
st.plotly_chart(fig_pie)
|
|
|
17 |
# 初始化一個空的 DataFrame 列表來儲存所有資料
|
18 |
df_list = []
|
19 |
|
|
|
20 |
# 迭代每個網址並抓取資料
|
21 |
for url in urls:
|
22 |
response = requests.get(url)
|
|
|
35 |
description_tag = soup.find('div', class_='restaurant-details__description--text')
|
36 |
description = description_tag.text.strip() if description_tag else 'N/A'
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
# 將抓取的資料新增到列表中
|
39 |
df_list.append({
|
40 |
'Title': title,
|
41 |
'Address': address,
|
42 |
'Phone': phone,
|
43 |
+
'Description': description
|
|
|
|
|
44 |
})
|
45 |
|
46 |
# 使用 pd.DataFrame() 將所有資料合併成一個 DataFrame
|
|
|
67 |
|
68 |
# 按鈕來顯示圓餅圖
|
69 |
if st.button('顯示每個區的商家數量比例圓餅圖'):
|
70 |
+
st.plotly_chart(fig_pie)
|