Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,10 @@
|
|
| 1 |
import requests
|
| 2 |
from bs4 import BeautifulSoup
|
| 3 |
import pandas as pd
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# 從Google Sheets讀取網址
|
| 7 |
sheet_id = "1IywohlfSnpPND45mUZQM8F1r7JNUiZzC9ZyH9jB7ufc" # 替換為實際的sheet ID
|
|
@@ -11,7 +14,7 @@ df_urls = pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export
|
|
| 11 |
urls = df_urls['URL'].tolist()
|
| 12 |
|
| 13 |
# 建立一個空的 DataFrame 來存放所有餐廳的資料
|
| 14 |
-
df = pd.DataFrame(columns=["Store Name", "Address", "Phone", "Description"])
|
| 15 |
|
| 16 |
# 迭代處理每個網址
|
| 17 |
for url in urls:
|
|
@@ -39,16 +42,28 @@ for url in urls:
|
|
| 39 |
except AttributeError:
|
| 40 |
description = None
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# 將每個餐廳的資料轉換為 DataFrame 並使用 pd.concat 合併
|
| 43 |
new_row = pd.DataFrame({
|
| 44 |
"Store Name": [store_name],
|
| 45 |
"Address": [address],
|
| 46 |
"Phone": [phone],
|
| 47 |
-
"Description": [description]
|
|
|
|
|
|
|
| 48 |
})
|
| 49 |
|
| 50 |
df = pd.concat([df, new_row], ignore_index=True)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
st.dataframe(df)
|
|
|
|
| 1 |
import requests
|
| 2 |
from bs4 import BeautifulSoup
|
| 3 |
import pandas as pd
|
| 4 |
+
from geopy.geocoders import Nominatim
|
| 5 |
+
|
| 6 |
+
# 初始化 geopy 的 Nominatim geolocator
|
| 7 |
+
geolocator = Nominatim(user_agent="geoapiExercises")
|
| 8 |
|
| 9 |
# 從Google Sheets讀取網址
|
| 10 |
sheet_id = "1IywohlfSnpPND45mUZQM8F1r7JNUiZzC9ZyH9jB7ufc" # 替換為實際的sheet ID
|
|
|
|
| 14 |
urls = df_urls['URL'].tolist()
|
| 15 |
|
| 16 |
# 建立一個空的 DataFrame 來存放所有餐廳的資料
|
| 17 |
+
df = pd.DataFrame(columns=["Store Name", "Address", "Phone", "Description", "Latitude", "Longitude"])
|
| 18 |
|
| 19 |
# 迭代處理每個網址
|
| 20 |
for url in urls:
|
|
|
|
| 42 |
except AttributeError:
|
| 43 |
description = None
|
| 44 |
|
| 45 |
+
# 使用 geopy 取得經緯度
|
| 46 |
+
latitude, longitude = None, None
|
| 47 |
+
if address:
|
| 48 |
+
try:
|
| 49 |
+
location = geolocator.geocode(address)
|
| 50 |
+
if location:
|
| 51 |
+
latitude = location.latitude
|
| 52 |
+
longitude = location.longitude
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Error getting geocode for address {address}: {e}")
|
| 55 |
+
|
| 56 |
# 將每個餐廳的資料轉換為 DataFrame 並使用 pd.concat 合併
|
| 57 |
new_row = pd.DataFrame({
|
| 58 |
"Store Name": [store_name],
|
| 59 |
"Address": [address],
|
| 60 |
"Phone": [phone],
|
| 61 |
+
"Description": [description],
|
| 62 |
+
"Latitude": [latitude],
|
| 63 |
+
"Longitude": [longitude]
|
| 64 |
})
|
| 65 |
|
| 66 |
df = pd.concat([df, new_row], ignore_index=True)
|
| 67 |
|
| 68 |
+
# 輸出 DataFrame
|
| 69 |
+
print(df)
|
|
|