Roberta2024 commited on
Commit
89cae94
·
verified ·
1 Parent(s): dfe4085

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
+ import plotly.express as px
5
+ import streamlit as st
6
+
7
+ # Streamlit 應用程式標題
8
+ st.title("各區餐廳數量分佈分析")
9
+
10
+ # 從 Google 試算表中讀取 URLs
11
+ sheet_id = "1SvHM_eV2hoPcOEB4bOnrUMbUCzslPnmHEn6qI7WuOqk"
12
+ urls_df = pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv")
13
+
14
+ # 將 URLs 轉換為列表
15
+ urls = urls_df['網址'].tolist() # 假設表格中的URL列名為"網址"
16
+
17
+ # 初始化一個空的 DataFrame 列表來儲存所有資料
18
+ df_list = []
19
+
20
+ # 迭代每個網址並抓取資料
21
+ for url in urls:
22
+ response = requests.get(url)
23
+ soup = BeautifulSoup(response.content, 'html.parser')
24
+
25
+ # 解析並抓取所需資料
26
+ title = soup.find('h1', class_='restaurant-details__heading--title').text.strip()
27
+ address = soup.find('li', class_='restaurant-details__heading--address').text.strip()
28
+
29
+ # 手機號碼處理
30
+ phone_tag = soup.find('a', {'data-event': 'CTA_tel'})
31
+ phone = phone_tag['href'].replace('tel:', '') if phone_tag else 'N/A'
32
+
33
+ description = soup.find('div', class_='restaurant-details__description--text').text.strip()
34
+
35
+ # 將抓取的資料新增到列表中
36
+ df_list.append({'Title': title, 'Address': address, 'Phone': phone, 'Description': description})
37
+
38
+ # 使用 pd.DataFrame() 將所有資料合併成一個 DataFrame
39
+ df = pd.DataFrame(df_list)
40
+
41
+ # 從地址中提取區域
42
+ df['District'] = df['Address'].str.extract(r'(\w+區)')
43
+
44
+ # 統計每個區的商家數量
45
+ district_counts = df['District'].value_counts().reset_index()
46
+ district_counts.columns = ['District', 'Count']
47
+
48
+ # 繪製柱狀圖
49
+ fig_bar = px.bar(district_counts, x='District', y='Count', title='各區餐廳數量分佈')
50
+ fig_bar.update_layout(xaxis_title='區域', yaxis_title='餐廳數量')
51
+
52
+ # 繪製圓餅圖
53
+ fig_pie = px.pie(district_counts, values='Count', names='District', title='各區餐廳比例')
54
+
55
+ # 使用 Streamlit 顯示圖表
56
+ st.plotly_chart(fig_bar)
57
+ st.plotly_chart(fig_pie)
58
+
59
+ # 顯示統計結果
60
+ st.write("各區餐廳數量統計表")
61
+ st.dataframe(district_counts)
62
+
63
+ # 顯示原始數據框
64
+ st.write("原始餐廳資料")
65
+ st.dataframe(df)