PhilipL commited on
Commit
daa5b91
·
verified ·
1 Parent(s): aba5539

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -43
app.py CHANGED
@@ -82,67 +82,66 @@ def sync_google_maps(url):
82
  try:
83
  yield "🚀 [1/4] 啟動瀏覽器..."
84
  with sync_playwright() as p:
 
85
  browser = p.chromium.launch(headless=True)
86
- page = browser.new_page()
87
- yield "🌐 [2/4] 連線中..."
88
- page.goto(clean_url, wait_until="domcontentloaded", timeout=60000)
89
- yield "⏳ [3/4] 等待列表加載 (10秒)..."
90
- time.sleep(10)
91
 
92
- yield "📄 [4/4] 解析餐廳資訊與圖片..."
 
93
 
94
- # 找到所有餐廳的容器 (通常在 Saved Places 中是 div[role="article"])
95
- # 若失效,請檢查 Google Maps 頁面原始碼
96
- items = page.locator('div[role="article"]').all()
 
 
 
 
 
97
 
98
- if not items:
99
- # 備用方案:尋找包含名稱的父級容器
100
- items = page.locator('div.m67Hec').all()
 
 
 
 
 
 
 
 
 
 
 
101
 
102
  restaurant_list = []
103
- for item in items:
104
- try:
105
- # 提取名稱
106
- name_el = item.locator('div.fontHeadlineSmall')
107
- if name_el.count() == 0: continue
108
- name = name_el.inner_text().strip()
109
-
110
- # 提取地址
111
- addr_el = item.locator('div.fontBodyMedium')
112
- addr = addr_el.inner_text().strip() if addr_el.count() > 0 else "詳情見地圖"
113
-
114
- # 提取圖片 URL (通常是 img 標籤的 src)
115
- img_el = item.locator('img')
116
- img_url = None
117
- if img_el.count() > 0:
118
- img_url = img_el.get_attribute('src')
119
-
120
- # 處理無圖片或 Base64 縮圖的情況
121
- if not img_url or "http" not in img_url:
122
- img_url = "https://images.unsplash.com/photo-1517248135467-4c7edcad34c4?w=500" # 預設圖片
123
-
124
- restaurant_list.append({
125
- "Name": name,
126
- "Address": addr,
127
- "Image_URL": img_url,
128
- "RAG_Content": f"餐廳:{name},地址:{addr}",
129
- "URL": f"https://www.google.com/search?q={urllib.parse.quote(name)}"
130
- })
131
- except:
132
- continue
133
 
134
  browser.close()
135
 
136
  if not restaurant_list:
137
- yield "⚠️ 找不到餐廳。請確認連結已設為「共用」並包含已儲存地點。"
138
  return
139
 
 
140
  global_df = pd.DataFrame(restaurant_list)
141
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
142
  vectorstore = FAISS.from_texts(global_df['RAG_Content'].tolist(), embeddings, metadatas=global_df.to_dict('records'))
143
  global_retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
144
  rag_initialized = True
145
  yield f"✅ 同步成功!共有 {len(restaurant_list)} 間餐廳。"
 
146
  except Exception as e:
147
  yield f"❌ 系統錯誤:{str(e)}"
148
 
 
82
  try:
83
  yield "🚀 [1/4] 啟動瀏覽器..."
84
  with sync_playwright() as p:
85
+ # 使用更真實的 User-Agent 避免被擋
86
  browser = p.chromium.launch(headless=True)
87
+ context = browser.new_context(
88
+ user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
89
+ )
90
+ page = context.new_page()
 
91
 
92
+ yield "🌐 [2/4] 連線中並等待列表出現..."
93
+ page.goto(clean_url, wait_until="networkidle", timeout=60000)
94
 
95
+ # 強制等待關鍵元素出現 (Google Maps 列表項目的常見特徵)
96
+ try:
97
+ page.wait_for_selector('div[role="article"]', timeout=15000)
98
+ except:
99
+ # 如果找不到 article,嘗試找尋包含標題的 div
100
+ page.wait_for_selector('.fontHeadlineSmall', timeout=10000)
101
+
102
+ yield "⏳ [3/4] 正在掃描清單與圖片..."
103
 
104
+ # 模擬微幅捲動以確保懶加載圖片有被讀取
105
+ page.mouse.wheel(0, 5000)
106
+ time.sleep(2)
107
+
108
+ # 使用 JavaScript 直接提取資料,這比 Playwright Locator 更穩定
109
+ results = page.evaluate("""() => {
110
+ const items = Array.from(document.querySelectorAll('div[role="article"], .m67Hec'));
111
+ return items.map(item => {
112
+ const name = item.querySelector('.fontHeadlineSmall')?.innerText || "";
113
+ const addr = item.querySelector('.fontBodyMedium')?.innerText || "詳情見地圖";
114
+ const img = item.querySelector('img')?.src || "";
115
+ return { name, addr, img };
116
+ }).filter(x => x.name.length > 0);
117
+ }""")
118
 
119
  restaurant_list = []
120
+ for res in results:
121
+ name = res['name']
122
+ # 建立結構化資料
123
+ restaurant_list.append({
124
+ "Name": name,
125
+ "Address": res['addr'],
126
+ "Image_URL": res['img'] if res['img'].startswith('http') else "https://images.unsplash.com/photo-1517248135467-4c7edcad34c4?w=500",
127
+ "RAG_Content": f"餐廳:{name},地址:{res['addr']}",
128
+ "URL": f"https://www.google.com/search?q={urllib.parse.quote(name)}"
129
+ })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
  browser.close()
132
 
133
  if not restaurant_list:
134
+ yield "⚠️ 偵測到空列表。請確認網址是否正確,或嘗試將連結改為『公開』。"
135
  return
136
 
137
+ # 更新全域變數
138
  global_df = pd.DataFrame(restaurant_list)
139
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
140
  vectorstore = FAISS.from_texts(global_df['RAG_Content'].tolist(), embeddings, metadatas=global_df.to_dict('records'))
141
  global_retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
142
  rag_initialized = True
143
  yield f"✅ 同步成功!共有 {len(restaurant_list)} 間餐廳。"
144
+
145
  except Exception as e:
146
  yield f"❌ 系統錯誤:{str(e)}"
147