gpt4 book ai didi

javascript - Puppeteer 无法获取完整的源代码

转载 作者:搜寻专家 更新时间:2023-11-01 00:48:15 24 4
gpt4 key购买 nike

我正在使用 Node.js 和 Puppeteer 创建一个简单的抓取应用程序。我要抓取的页面是 this 。下面是我现在正在使用的代码。

const url = `https://www.betrebels.gr/el/sports?catids=122,40,87,28,45,2&champids=423,274616,1496978,1484069,1484383,465990,465991,91,71,287,488038,488076,488075,1483480,201,2,367,38,1481454,18,226,440,441,442,443,444,445,446,447,448,449,451,452,453,456,457,458,459,460,278261&datefilter=TodayTomorrow&page=prelive`
await page.goto(url, {waitUntil: 'networkidle2'});
let content: string = await page.content();
await page.screenshot({path: 'page.png',fullPage: true});
await fs.writeFile("temp.html", content);
//...Analyze the html and other stuff.

我得到的屏幕截图是 this,这正是我所期待的。

另一方面,页面内容很少,不代表图像上的数据。

我做错了什么吗?我没有正确等待 Javascript 完成吗?

enter image description here

最佳答案

该页面正在使用框架。您只能看到页面的主要内容(没有框架的内容)。要同时获取框架的内容,您需要首先找到框架(例如通过 page.$ ),然后通过 elementHandle.contentFrame 获取其框架句柄。 .然后您可以调用 frame.content()获取框架的内容。

简单示例

const frameElementHandle = await page.$('#selector iframe');
const frame = await frameElementHandle.contentFrame();
const frameContent = await frame.content();

根据页面的结构,您需要对多个框架执行此操作以获取所有内容,或者您​​甚至需要对框架内的框架执行此操作(给定页面似乎就是这种情况)。

读取所有框架内容的示例

下面是一个递归读取页面上所有框架的内容的例子。

const contents = [];
async function extractFrameContents(pageOrFrame) {
const frames = await pageOrFrame.$$('iframe');
for (let frameElement of frames) {
const frame = await frameElement.contentFrame();
const frameContent = await frame.content();

// do something with the content, example:
contents.push(frameContent);

// recursively repeat
await extractFrameContents(frame);
}
}
await extractFrameContents(page);

关于javascript - Puppeteer 无法获取完整的源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55994249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com