[英]JSDOM: Access divs inside iframe
我嘗試從具有以下html的網站http://www.example.com
抓取一些信息:
<!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內的href
和src
內容。
但是由於某種原因,輸出為:
iframe對象為null
iframe對象為null
你有什么想法如何熱內的IFRAME訪問HTML?
您需要采取不同的方法。 只需使用無頭瀏覽器在頁面加載期間通過網絡手動獲取數據並分別進行處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.