Rozeeeee commited on
Commit
615048f
·
verified ·
1 Parent(s): b84601b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -24
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
- try:
23
- response = requests.get(url)
24
- response.raise_for_status() # Raises an HTTPError for bad responses
25
- soup = BeautifulSoup(response.content, 'html.parser')
26
-
27
- # 解析並抓取所需資料
28
- title_tag = soup.find('h1', class_='restaurant-details__heading--title')
29
- title = title_tag.text.strip() if title_tag else 'N/A'
30
-
31
- address_tag = soup.find('li', class_='restaurant-details__heading--address')
32
- address = address_tag.text.strip() if address_tag else 'N/A'
33
-
34
- phone_tag = soup.find('a', {'data-event': 'CTA_tel'})
35
- phone = phone_tag['href'].replace('tel:', '') if phone_tag else 'N/A'
36
-
37
- description_tag = soup.find('div', class_='restaurant-details__description--text')
38
- description = description_tag.text.strip() if description_tag else 'N/A'
39
-
40
- # 將抓取的資料新增到列表中
41
- df_list.append({'Title': title, 'Address': address, 'Phone': phone, 'Description': description})
42
-
43
- except requests.exceptions.HTTPError as e:
44
- print(f"HTTP error occurred for URL {url}: {e}")
45
- continue # Skip to the next URL if there's an error
 
 
 
 
 
 
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']])