页面加载后的 Cheerio 选择器
Cheerio selector after page loaded
我想抓取一个url这个网站iframe的值: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)
}
}
this is video iframe
您不能为此使用 cheerio。 Cheerio 不是动态的,只会加载 html 从请求返回的任何内容。
查看您的网页,大部分内容都是异步加载的,因此初始 html 会很空。
此外,视频源在进入浏览器时会延迟加载 window。所以你必须使用一个实际的无头浏览器来完成任务。这是一个例子:
// 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);
})();
我想抓取一个url这个网站iframe的值: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)
}
}
this is video iframe
您不能为此使用 cheerio。 Cheerio 不是动态的,只会加载 html 从请求返回的任何内容。
查看您的网页,大部分内容都是异步加载的,因此初始 html 会很空。
此外,视频源在进入浏览器时会延迟加载 window。所以你必须使用一个实际的无头浏览器来完成任务。这是一个例子:
// 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);
})();