deneve07 commited on
Commit
2ada690
·
verified ·
1 Parent(s): dd37c90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -33
app.py CHANGED
@@ -239,23 +239,24 @@ def get_japan_originator(ing_ja, page):
239
  log, brands, companies = [], set(), set()
240
  try:
241
  log.append(f"1. 前往 PMDA (搜尋: {ing_ja})...")
242
- page.goto("https://www.pmda.go.jp/PmdaSearch/iyakuSearch/", timeout=30000, wait_until="domcontentloaded")
243
 
244
- log.append("2. 「一般名(成分)」欄位並填寫...")
245
- search_input = page.locator('input#txtIppanName, input[name="general_name"]').first
246
- search_input.wait_for(state="visible", timeout=15000)
247
- search_input.fill(ing_ja)
 
248
 
249
- log.append("3. 監聽新視窗並按下 Enter...")
250
  with page.expect_popup() as popup_info:
251
  search_input.press("Enter")
252
  popup = popup_info.value
253
 
254
- log.append("4. 等待新視窗表格並掃描所有分頁...")
255
  current_page = 1
256
 
257
- # 💡 新增:自動翻頁迴圈
258
  while True:
 
259
  popup.wait_for_selector('table#ResultList, .errormsg, .non-result', timeout=15000)
260
  soup = BeautifulSoup(popup.content(), 'html.parser')
261
  table = soup.find('table', id='ResultList')
@@ -267,6 +268,7 @@ def get_japan_originator(ing_ja, page):
267
  tds = tr.find_all('td')
268
  if len(tds) >= 3:
269
  title = tds[1].get_text(strip=True)
 
270
  if not is_generic(title, "", ing_ja):
271
  brands.add(clean_brand_name(title))
272
  companies.add(tds[2].get_text(separator=" ", strip=True).replace('製造販売元/', ''))
@@ -274,15 +276,14 @@ def get_japan_originator(ing_ja, page):
274
  log.append(f"❌ 第 {current_page} 頁未出現 ResultList。")
275
  break
276
 
277
- # 檢查是否有下一頁 (透過檢查下一頁的 JavaScript 觸發連結)
278
  current_page += 1
279
  next_page_link = popup.locator(f'a[href="javascript:changePg({current_page});"]')
280
 
281
  if next_page_link.count() > 0:
282
- log.append(f" -> 發現第 {current_page} 頁,準備翻頁...")
283
- next_page_link.first.click()
284
- popup.wait_for_load_state('domcontentloaded')
285
- popup.wait_for_timeout(1500) # 給予表格重新渲染的緩衝時間
286
  else:
287
  log.append(" -> 已無下一頁,結束掃描。")
288
  break
@@ -298,30 +299,24 @@ def get_switzerland_originator(ing_de, page):
298
  log, brands, companies = [], set(), set()
299
  try:
300
  log.append(f"1. 前往 Swissmedicinfo 搜尋頁面 (搜尋: {ing_de})...")
301
- page.goto("https://swissmedicinfo.ch/SearchPage", timeout=30000)
302
 
303
- # 💡 修正:透過 Label 的 for 屬性反查 Input 的 ID,這是最堅固的定
304
- log.append("2. 尋找 Wirkstoff (成分) 標籤並擷綁定 ID...")
305
- label = page.locator('label', has_text='Wirkstoff').first
306
- label.wait_for(state="visible", timeout=15000)
307
 
308
- input_id = label.get_attribute('for')
309
- if input_id:
310
- log.append(f" -> 成功鎖定輸入框 ID: {input_id}")
311
- ing_input = page.locator(f'#{input_id}')
312
- else:
313
- log.append(" -> 找不到 for 屬性,啟動備用相鄰節點定位...")
314
- ing_input = page.locator('input[type="text"]').nth(1)
315
-
316
- ing_input.fill(ing_de)
317
 
318
- log.append("3. 點擊 Packungsbeilage suchen (搜尋) 按鈕...")
319
- search_btn = page.locator('button', has_text='Packungsbeilage suchen').first
320
- search_btn.click()
321
 
322
- log.append("4. 等待網路請求與卡片 (.medicament-card) 渲染...")
323
- page.wait_for_load_state('networkidle', timeout=15000)
324
- page.wait_for_timeout(3000) # 給予 Vue.js 長出卡片的緩衝時間
 
325
 
326
  soup = BeautifulSoup(page.content(), 'html.parser')
327
  cards = soup.find_all('div', class_=re.compile('medicament-card'))
 
239
  log, brands, companies = [], set(), set()
240
  try:
241
  log.append(f"1. 前往 PMDA (搜尋: {ing_ja})...")
242
+ page.goto("https://www.pmda.go.jp/PmdaSearch/iyakuSearch/", timeout=45000, wait_until="domcontentloaded")
243
 
244
+ log.append("2. 準確位置首頁搜尋框 (id='txtName')...")
245
+ # 依照您提供的 HTML 結構,直接鎖定 txtName
246
+ search_input = page.locator('input#txtName')
247
+ search_input.wait_for(state="attached", timeout=15000)
248
+ search_input.fill(ing_ja, force=True)
249
 
250
+ log.append("3. 觸發搜尋並監聽彈出新視窗...")
251
  with page.expect_popup() as popup_info:
252
  search_input.press("Enter")
253
  popup = popup_info.value
254
 
255
+ log.append("4. 等待新視窗表格並啟動自動翻頁掃描...")
256
  current_page = 1
257
 
 
258
  while True:
259
+ # 等待表格出現
260
  popup.wait_for_selector('table#ResultList, .errormsg, .non-result', timeout=15000)
261
  soup = BeautifulSoup(popup.content(), 'html.parser')
262
  table = soup.find('table', id='ResultList')
 
268
  tds = tr.find_all('td')
269
  if len(tds) >= 3:
270
  title = tds[1].get_text(strip=True)
271
+ # 這裡會自動把帶有「括號」或廠商名稱的學名藥濾掉
272
  if not is_generic(title, "", ing_ja):
273
  brands.add(clean_brand_name(title))
274
  companies.add(tds[2].get_text(separator=" ", strip=True).replace('製造販売元/', ''))
 
276
  log.append(f"❌ 第 {current_page} 頁未出現 ResultList。")
277
  break
278
 
279
+ # 尋找下一頁的按鈕 (例如 javascript:changePg(2);)
280
  current_page += 1
281
  next_page_link = popup.locator(f'a[href="javascript:changePg({current_page});"]')
282
 
283
  if next_page_link.count() > 0:
284
+ log.append(f" -> 發現第 {current_page} 頁,執行翻頁...")
285
+ next_page_link.first.click(force=True)
286
+ popup.wait_for_timeout(2000) # 給予表格重新渲染的緩衝時間
 
287
  else:
288
  log.append(" -> 已無下一頁,結束掃描。")
289
  break
 
299
  log, brands, companies = [], set(), set()
300
  try:
301
  log.append(f"1. 前往 Swissmedicinfo 搜尋頁面 (搜尋: {ing_de})...")
302
+ page.goto("https://swissmedicinfo.ch/SearchPage", timeout=45000, wait_until="domcontentloaded")
303
 
304
+ log.append("2. 透過父層容器定位 Wirkstoff ...")
305
+ # 絕對定位:尋找包含 Wirkstoff Vue 欄位容器,抓內部的 input,無視任何 ID
306
+ search_input = page.locator('div.v-field__field').filter(has_text="Wirkstoff").locator('input[type="text"]').first
 
307
 
308
+ # 💡 關鍵修正:改為等待 attached (存在於DOM),並用 force=True 無視 UI 動畫遮罩強制填寫
309
+ search_input.wait_for(state="attached", timeout=15000)
310
+ search_input.fill(ing_de, force=True)
 
 
 
 
 
 
311
 
312
+ log.append("3. 強制點擊搜尋按鈕...")
313
+ search_btn = page.locator('button').filter(has_text='Packungsbeilage suchen').first
314
+ search_btn.click(force=True)
315
 
316
+ log.append("4. 等待卡片 (.medicament-card) 渲染...")
317
+ page.wait_for_timeout(3000) # 給予 Vue.js 充分時間生成卡片
318
+ # 確保畫面卡片,或者出現找不到資料提示
319
+ page.wait_for_selector('.medicament-card, .v-alert', timeout=15000)
320
 
321
  soup = BeautifulSoup(page.content(), 'html.parser')
322
  cards = soup.find_all('div', class_=re.compile('medicament-card'))