Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ 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("餐廳資料抓取與分析")
|
@@ -17,32 +18,41 @@ urls = urls_df['網址'].tolist()
|
|
17 |
# 初始化一個空的 DataFrame 列表來儲存所有資料
|
18 |
df_list = []
|
19 |
|
|
|
|
|
|
|
20 |
# 迭代每個網址並抓取資料
|
21 |
for url in urls:
|
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 |
# 使用 pd.DataFrame() 將所有資料合併成一個 DataFrame
|
48 |
df = pd.DataFrame(df_list)
|
@@ -69,3 +79,11 @@ fig_pie.update_layout(title='每個區的商家數量比例')
|
|
69 |
# 按鈕來顯示圓餅圖
|
70 |
if st.button('顯示每個區的商家數量比例圓餅圖'):
|
71 |
st.plotly_chart(fig_pie)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import pandas as pd
|
4 |
import plotly.graph_objects as go
|
5 |
import streamlit as st
|
6 |
+
from geopy.geocoders import Nominatim # New import for geocoding
|
7 |
|
8 |
# 設定應用標題
|
9 |
st.title("餐廳資料抓取與分析")
|
|
|
18 |
# 初始化一個空的 DataFrame 列表來儲存所有資料
|
19 |
df_list = []
|
20 |
|
21 |
+
# 初始化地理定位器
|
22 |
+
geolocator = Nominatim(user_agent="restaurant_locator") # Initialize geolocator
|
23 |
+
|
24 |
# 迭代每個網址並抓取資料
|
25 |
for url in urls:
|
26 |
+
response = requests.get(url)
|
27 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
28 |
+
|
29 |
+
# 解析並抓取所需資料
|
30 |
+
title_tag = soup.find('h1', class_='restaurant-details__heading--title')
|
31 |
+
title = title_tag.text.strip() if title_tag else 'N/A'
|
32 |
+
|
33 |
+
address_tag = soup.find('li', class_='restaurant-details__heading--address')
|
34 |
+
address = address_tag.text.strip() if address_tag else 'N/A'
|
35 |
+
|
36 |
+
phone_tag = soup.find('a', {'data-event': 'CTA_tel'})
|
37 |
+
phone = phone_tag['href'].replace('tel:', '') if phone_tag else 'N/A'
|
38 |
+
|
39 |
+
description_tag = soup.find('div', class_='restaurant-details__description--text')
|
40 |
+
description = description_tag.text.strip() if description_tag else 'N/A'
|
41 |
+
|
42 |
+
# Geocode address to get latitude and longitude
|
43 |
+
location = geolocator.geocode(address) if address != 'N/A' else None
|
44 |
+
lat = location.latitude if location else None
|
45 |
+
lon = location.longitude if location else None
|
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)
|
|
|
79 |
# 按鈕來顯示圓餅圖
|
80 |
if st.button('顯示每個區的商家數量比例圓餅圖'):
|
81 |
st.plotly_chart(fig_pie)
|
82 |
+
|
83 |
+
# 顯示地圖
|
84 |
+
st.subheader("餐廳地圖")
|
85 |
+
# Drop rows with missing coordinates
|
86 |
+
df_map = df.dropna(subset=['Latitude', 'Longitude'])
|
87 |
+
|
88 |
+
# 顯示地圖
|
89 |
+
st.map(df_map[['Latitude', 'Longitude']])
|