[官方認證] LINESPOT 留言抓取 iframe 解析

Hi All,

在官方認證題目 LINESPOT 留言抓取 題目中

因為需要操作的網頁節點在 iframe 中

同時該網頁已改版,iframe 內嵌的網頁與母頁面的 domain 不同

造成無法單純使用以下方式來取得 iframe 頁面內的節點 (按鈕 & 資料)

let btn = document.querySelector('iframe').contentWindow.document.querySelector('button[class="btn-more"]')

調整後作法

  1. 先取得 iframe 內嵌網頁的網址
  2. 再直接前往 內嵌網頁的網址
  3. 再操作 點擊按鈕、抓取留言 等動作

1. 取得 iframe 內嵌網頁的網址

請使用以下腳本來完成

// 取得 iframe 父節點 (父節點中有網頁的關鍵資訊)
let iframeParent = document.querySelector('iframe').parentNode

// 取得 content ID (iframe URL 中的關鍵資訊)
let contentId = iframeParent.getAttribute('data-contentid')

// 利用 content ID 組合出 iframe url
let url = `https://waku.line-apps.com/?clientId=1582347558&href=https%3A%2F%2Fspot.line.me%2Fdetail%2F484051118033234000&origin=https%3A%2F%2Fspot.line.me&v=1.0.14&subwindow=false&isliff=false&contentId=${contentId}&contentUrl=https%3A%2F%2Fspot.line.me%2Fdetail%2F${contentId}%3Futm_source%3DLINETimeline%26utm_medium%3DTLROP%26utm_campaign%3DPOI_ID&preData=true&ignoreLike=true&ignoreShare=true&removeReviewTitle=true&postButtonColor=%2315BD66&ratingStarColor=%23FF6F37`

// 寫出為文字檔 iframe_url.txt
write('iframe_url.txt', url)

2. 前往 iframe 內嵌頁面

後續就可以正常使用腳本操作頁面(網頁沒辦法手動 Scroll,可利用 element.scrollIntoView() 來捲動)