Rozeeeee commited on
Commit
c0beac8
·
verified ·
1 Parent(s): cabd228

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +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
+ # 從 Google 試算表中讀取 URLs
11
+ sheet_id = "1W20lawjiQtEpljUKoEaMVPDlSdnhvJLPUy2jk5xao_w"
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()
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
+ st.subheader("抓取的餐廳資料")
43
+ st.dataframe(df)
44
+
45
+ # 統計每個區的商家數量
46
+ df['Area'] = df['Address'].str.extract(r'(\w+區)') # 提取區域
47
+ area_counts = df['Area'].value_counts() # 統計各區的商家數量
48
+
49
+ # 繪製柱狀圖
50
+ fig_bar = go.Figure(data=[go.Bar(x=area_counts.index, y=area_counts.values)])
51
+ fig_bar.update_layout(title='每個區的商家數量', xaxis_title='區域', yaxis_title='商家數量')
52
+
53
+ # 顯示柱狀圖
54
+ st.plotly_chart(fig_bar)
55
+
56
+ # 繪製圓餅圖
57
+ fig_pie = go.Figure(data=[go.Pie(labels=area_counts.index, values=area_counts.values)])
58
+ fig_pie.update_layout(title='每個區的商家數量比例')
59
+
60
+ # 顯示圓餅圖
61
+ st.plotly_chart(fig_pie)