页面加载后的Cheerio选择器



我想在此网站中抓取iframe的url值:https://lk21online.digital/nonton-profile-2021-subtitle-indonesia/
当我从视图页面源搜索iframe时,没有找到它,我认为iframe是在javascript加载页面后加载的
或者我的选择器错误
请有人帮我检查我的选择器或我需要为我的代码做什么
对不起我的英语不好。。。

这是我的代码:

async function getDetail(res, url) {
try {
const html = await scraping(res, url)
const $ = cheerio.load(html)
const article = $('#site-container #content .gmr-maincontent #primary #main .gmr-box-content #muvipro_player_content_id #player1-tab-content')
let result = []
setTimeout(() => {
article.each(function () {
const title = $(this).find('.item-article h2').text()
const watch = $(this).find('iframe').attr('src')
result.push({
title,
watch,
})
})
res.json({ result })
}, 5000)

}
catch (err) {
console.log(err)
}
}

这是视频iframe

您不能为此使用cheerio。Cheerio不是动态的,只是加载请求返回的任何html。

查看您的网页,大多数内容都是异步加载的,因此初始的html将非常空。

此外,视频源在进入浏览器窗口时会延迟加载。因此,您必须使用实际的无头浏览器来完成任务。这里有一个例子:

// iframeUrl.js
const puppeteer = require("puppeteer");
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Goto page
await page.goto("https://lk21online.digital/nonton-profile-2021-subtitle-indonesia/");
// Scroll down
page.evaluate((_) => window.scrollBy(0, 1000));
// Wait a bit
await new Promise((resolve) => setTimeout(resolve, 5000));
// Get the src of the iframe
const iframeUrl = await page.evaluate(`$("#player1-tab-content iframe").attr("src")`);
console.log(iframeUrl);
await browser.close();
process.exit(0);
})();

最新更新