2017-08-29 281 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

您需要以不同的方式处理它。只需使用无头浏览器手动在网页加载过程中通过网络获取数据并分别处理。