javascript - 使用 Puppeteer 为图像抓取无限滚动网页,但在等待异步中不返回任何内容
问题描述
所以我正在使用 node.js 和 Puppeteer 尝试从无限滚动的网页中抓取一定数量的图像 URL(我只是使用 reddit 主页进行测试,但如果你愿意,可以替换为你自己的),但是应该容纳它们的数组现在是空的。
我什至把它全部放在一个匿名异步函数中,这样我就可以强制它await
,以防时间问题:
const puppeteer = require('puppeteer');
var pBrowser = await puppeteer.launch();
console.log("\t* Browser launched");
var pPage = await pBrowser.newPage();
console.log("\t* Page launched");
let sUrl = foo;
await pPage.goto(sUrl);
let iItemCount = 10;
let tImageSrcs = [];
await async function () {
let iPreviousHeight;
console.log("Starting search at the top");
while (tImageSrcs.length < iItemCount) {
tImageSrcs = await pPage.evaluate( function() {
let tItems = Array.from(document.images, e => e.src );
console.log("\t\t* "+tItems.length+" images overall found within this section; trimming...");
tItems = tItems.filter( sImage => [".jpg",".png"].includes(sImage.substring(sImage.length-4)) );
console.log("\t\t* "+tItems.length+" images after filtering");
return tImageSrcs.concat(tItems);
});
console.log("\t* "+tImageSrcs.length+" appropriate images sourced so far...");
iPreviousHeight = await pPage.evalutate("document.body.scrollHeight");
await pPage.evaluate('window.scrollTo(0, document.body.scrollHeight)');
console.log("Searching at scroll height "+iPreviousHeight);
await pPage.waitForFunction(`document.body.scrollHeight > ${iPreviousHeight}`);
await page.waitFor(1000);
}
};
console.log("\t* "+tImageSrcs.length+" images sourced");
但似乎它甚至没有在 async 函数中调用任何东西,因为无论它是否能够找到任何图像,输出甚至都不包含任何console.log
语句,包括位于(几乎)最顶部的语句异步函数:
* Browser launched
* Page launched
* 0 images sourced
解决方案
一些注意事项:
- 您创建了异步函数,但您没有调用它,因此它没有被执行:
await async function () { /*...*/ }
应该:
await async function () { /*...*/ }()
无论如何,这个包装器函数是多余的:我想你在使用时已经在一个异步函数中
await
,所以不需要使用包装器。你调用
console.log("\t* "+tImageSrcs.length+" images sourced");
whentImageSrcs
仍然是空的,因为上面的函数没有被执行(await
只是等待它的创建,而不是执行)。tImageSrcs
内部pPage.evaluate()
代码未定义(tImageSrcs
在 Node.js 上下文中是一个变量,在浏览器上下文中不可用)。您需要通过其可序列化的值来传输它。
所以试试这个变种:
const puppeteer = require('puppeteer');
var pBrowser = await puppeteer.launch();
console.log("\t* Browser launched");
var pPage = await pBrowser.newPage();
console.log("\t* Page launched");
let sUrl = foo;
await pPage.goto(sUrl);
let iItemCount = 10;
let tImageSrcs = [];
let iPreviousHeight;
console.log("Starting search at the top");
while (tImageSrcs.length < iItemCount) {
tImageSrcs = await pPage.evaluate( function(srcs) {
let tItems = Array.from(document.images, e => e.src );
console.log("\t\t* "+tItems.length+" images overall found within this section; trimming...");
tItems = tItems.filter( sImage => [".jpg",".png"].includes(sImage.substring(sImage.length-4)) );
console.log("\t\t* "+tItems.length+" images after filtering");
return srcs.concat(tItems);
}, tImageSrcs);
console.log("\t* "+tImageSrcs.length+" appropriate images sourced so far...");
iPreviousHeight = await pPage.evalutate("document.body.scrollHeight");
await pPage.evaluate('window.scrollTo(0, document.body.scrollHeight)');
console.log("Searching at scroll height "+iPreviousHeight);
await pPage.waitForFunction(`document.body.scrollHeight > ${iPreviousHeight}`);
await page.waitFor(1000);
}
console.log("\t* "+tImageSrcs.length+" images sourced");
推荐阅读
- snakemake - Snakemake 运行子工作流程,但不运行我的工作流程的其余部分(直接转到全部规则)
- c++ - 如何从随机值范围内删除某个元素?
- visual-studio-code - 是否可以在不删除未使用的导入的情况下启用 source.organizeImports?
- android - 如何在颤振中创建三次单击按钮
- excel - 将文件夹中特定的excel文件复制到主工作表中
- r - 按字母顺序重新排列垂直轴并更改堆叠百分比条形图(ggplot2)的二进制变量的位置
- ruby - 如何在 Ruby 2.7.0 上安装 Nokogiri
- python - 我可以使用 pyspark 将复杂的 json 对象转换为 Azure Databricks 中数据框中的多行吗?
- mysql - 如何在 MySQL 中查找列是否为 auto_increment
- javascript - 错误:找不到模块 './components/fb-comment-embed/fb-comment-embed' - Angular Universal 与 ngx-facebook 模块