Roberta2024 commited on
Commit
8a76c02
·
verified ·
1 Parent(s): 6e99bbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -7
app.py CHANGED
@@ -4,6 +4,9 @@ from bs4 import BeautifulSoup
4
  import pandas as pd
5
  import plotly.express as px
6
  import base64
 
 
 
7
 
8
  # Function to set background image
9
  def set_background(png_file):
@@ -35,6 +38,7 @@ min_rating = st.slider("請輸入想查詢的最低評分:", 1.0, 5.0, 3.5)
35
  urls = [
36
  "https://www.tw-animal.com/pet/171211/c000196.html",
37
  "https://www.tw-animal.com/pet/171211/c000186.html",
 
38
  "https://www.tw-animal.com/pet/171211/c000081.html",
39
  "https://www.tw-animal.com/pet/171211/c001166.html",
40
  "https://www.tw-animal.com/pet/171211/c000773.html",
@@ -56,9 +60,13 @@ urls = [
56
  "https://www.tw-animal.com/pet/171211/c001252.html"
57
  ]
58
 
 
59
  # Create an empty list to store the extracted data
60
  data_list = []
61
 
 
 
 
62
  # Scrape data when the button is pressed
63
  if st.button('開始爬取資料'):
64
  st.write("正在爬取資料,請稍候...")
@@ -76,12 +84,20 @@ if st.button('開始爬取資料'):
76
 
77
  # Append the data to the list if rating meets the threshold
78
  if rating >= min_rating:
79
- data_list.append({
80
- "標題": title,
81
- "手機": phone,
82
- "地址": address,
83
- "評分": rating
84
- })
 
 
 
 
 
 
 
 
85
 
86
  # If data was scraped successfully
87
  if data_list:
@@ -109,6 +125,23 @@ if st.button('開始爬取資料'):
109
  pie_fig = px.pie(grouped_df, names='區域', values='評分', title="各區域寵物醫院比例")
110
  st.plotly_chart(pie_fig)
111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  # Sending notification to LINE
113
  if st.button('發送前五筆資料到Line'):
114
  msg = df1[:5].to_string(index=False)
@@ -128,4 +161,3 @@ if st.button('開始爬取資料'):
128
  st.success('資料已成功發送到 Line!')
129
  else:
130
  st.warning('沒有符合條件的資料。')
131
-
 
4
  import pandas as pd
5
  import plotly.express as px
6
  import base64
7
+ import folium
8
+ from streamlit_folium import st_folium
9
+ from geopy.geocoders import Nominatim
10
 
11
  # Function to set background image
12
  def set_background(png_file):
 
38
  urls = [
39
  "https://www.tw-animal.com/pet/171211/c000196.html",
40
  "https://www.tw-animal.com/pet/171211/c000186.html",
41
+ "https://www.tw-animal.com/pet/171211/c000186.html",
42
  "https://www.tw-animal.com/pet/171211/c000081.html",
43
  "https://www.tw-animal.com/pet/171211/c001166.html",
44
  "https://www.tw-animal.com/pet/171211/c000773.html",
 
60
  "https://www.tw-animal.com/pet/171211/c001252.html"
61
  ]
62
 
63
+
64
  # Create an empty list to store the extracted data
65
  data_list = []
66
 
67
+ # Initialize the geolocator
68
+ geolocator = Nominatim(user_agent="geoapiExercises")
69
+
70
  # Scrape data when the button is pressed
71
  if st.button('開始爬取資料'):
72
  st.write("正在爬取資料,請稍候...")
 
84
 
85
  # Append the data to the list if rating meets the threshold
86
  if rating >= min_rating:
87
+ try:
88
+ # Geocode the address to get the latitude and longitude
89
+ location = geolocator.geocode(address)
90
+ if location:
91
+ data_list.append({
92
+ "標題": title,
93
+ "手機": phone,
94
+ "地址": address,
95
+ "評分": rating,
96
+ "經度": location.longitude,
97
+ "緯度": location.latitude
98
+ })
99
+ except Exception as e:
100
+ st.warning(f"無法獲取經緯度:{e}")
101
 
102
  # If data was scraped successfully
103
  if data_list:
 
125
  pie_fig = px.pie(grouped_df, names='區域', values='評分', title="各區域寵物醫院比例")
126
  st.plotly_chart(pie_fig)
127
 
128
+ # Display the map
129
+ if st.button('顯示地圖'):
130
+ # Create a folium map centered around the average location
131
+ map_center = [df1['緯度'].mean(), df1['經度'].mean()]
132
+ pet_map = folium.Map(location=map_center, zoom_start=12)
133
+
134
+ # Add markers for each hospital
135
+ for index, row in df1.iterrows():
136
+ folium.Marker(
137
+ location=[row['緯度'], row['經度']],
138
+ popup=f"{row['標題']} (評分: {row['評分']})",
139
+ tooltip=row['標題']
140
+ ).add_to(pet_map)
141
+
142
+ # Render the map using streamlit_folium
143
+ st_folium(pet_map, width=700, height=500)
144
+
145
  # Sending notification to LINE
146
  if st.button('發送前五筆資料到Line'):
147
  msg = df1[:5].to_string(index=False)
 
161
  st.success('資料已成功發送到 Line!')
162
  else:
163
  st.warning('沒有符合條件的資料。')