2017-08-29 294 views
1

訪問的div我試圖颳去網站http://www.example.com一些信息具有以下HTML:JSDOM:裏面的iframe

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>My site</title> 
</head> 
<body> 
<div id="one"> 
    <div> 
     <iframe> 
      <!DOCTYPE html> 
      <html> 
      <head> 
      <meta charset="utf-8"> 
      <title>My site</title> 
      </head> 
      <body> 
      <div id="hello"> 
       <a href="http://example.net/somepage"><img src="http://example.net/dokuro_chan.jpg"></a> 
      </div> 
      </body> 
      </html> 
     </iframe> 
    </div> 
</div> 
<div id="two"> 
    <div> 
     <iframe> 
      <!DOCTYPE html> 
      <html> 
      <head> 
      <meta charset="utf-8"> 
      <title>My site</title> 
      </head> 
      <body> 
      <div id="hello"> 
       <a href="http://example.net/somepage2"><img src="http://example.net/dokuro_chan2.jpg"></a> 
      </div> 
      </body> 
      </html> 
     </iframe> 
    </div> 
</div> 
</body> 
</html> 

然後我嘗試使用jsdom刮通過的NodeJS iframe的內容:

const jsdom = require("jsdom"); 
const { JSDOM } = jsdom; 

JSDOM.fromURL("http://www.example.com",{ 
     resources: "usable", 
     runScripts: "dangerously" 
}).then(dom =>{ 

     const divIds=["#one","#two"] 

     divIds.forEach((divId)=> { 
      const selector=googleAdSelector(divId) 
      const iframe=dom.window.document.querySelector(selector) 
      console.log("Iframe Object", iframe) 
     }) 
     // callback(null,dom) 
}) 

const googleAdSelector=function(divId){ 
     return divId+" > div > iframe"; 
} 

我想讓tyo嘗試獲得的內容是獲取iframe中的hrefsrc內容。

但由於某些原因輸出:

iFrame對象空

iFrame對象空

你有什麼想法熱如何訪問HTML INSIDE的iframe?

回答

0

您需要以不同的方式處理它。只需使用無頭瀏覽器手動在網頁加載過程中通過網絡獲取數據並分別處理。