- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想迭代 CSV 文件并使用 puppeteer 截取 CSV 文件中每一行的 URL。
我有以下代码,工作正常,但每个请求都会等待前一个请求完成,因此需要很长时间才能运行:
const csv = require('csv-parser');
const fs = require('fs');
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const getFile = async function(rowId, path) {
const page = await browser.newPage();
page.setViewport({ width: 1000, height: 1500, deviceScaleFactor: 1 });
let url = 'https://www.facebook.com/ads/library/?id=' + rowId;
const response = await page.goto(url, { waitUntil: 'networkidle2' });
await page.waitFor(3000);
const body = await page.$('body');
await body.screenshot({
path: path
});
page.close();
};
let fname = 'ids.csv'
const csvPipe = fs.createReadStream(fname).pipe(csv());
csvPipe.on('data', async (row) => {
let id = row.ad_id;
console.log(id);
let path = './images/' + id + '.png';
csvPipe.pause();
await getFile(id, path);
csvPipe.resume();
}).on('end', () => {
console.log('CSV file successfully processed');
});
})();
如何使请求并行运行,以加快速度?
如果我删除 pause()
和 resume()
行,那么每次函数运行时我都会收到此错误:
(node:18610) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 14)
(node:18610) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'screenshot' of null
at getFile (/Users/me/Dropbox/Projects/scrape/index.js:29:12)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
最佳答案
这是一个并行运行用户可控数量的 getFile()
操作的方案。您将 maxInFlight 变量设置为要并行运行的页面数量(这可能只是您的内存使用情况或 facebook 可能应用的任何速率限制的问题)。您必须通过实验来决定将其设置为什么。我最初将其设置为 10,以允许 10 个页面同时“运行”。
这里的总体思路是,getFile()
递增/递减 inFlightCntr
作为一次打开页面数量的度量,然后根据 csvPipe 暂停或恢复在那个柜台上。
const csv = require('csv-parser');
const fs = require('fs');
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const maxInFlight = 10; // set this value to control how many pages run in parallel
let inFlightCntr = 0;
let paused = false;
async function getFile(rowId, path) {
try {
++inFlightCntr;
const page = await browser.newPage();
page.setViewport({ width: 1000, height: 1500, deviceScaleFactor: 1 });
let url = 'https://www.facebook.com/ads/library/?id=' + rowId;
const response = await page.goto(url, { waitUntil: 'networkidle2' });
await page.waitFor(3000);
const body = await page.$('body');
await body.screenshot({
path: path
});
await page.close();
} catch(e) {
console.log(e);
page.close();
} finally {
--inFlightCntr;
}
}
let fname = 'ids.csv'
const csvPipe = fs.createReadStream(fname).pipe(csv());
csvPipe.on('data', async (row) => {
let id = row.ad_id;
console.log(id);
let path = './images/' + id + '.png';
getFile(id, path).finally(() => {
if (paused && inFlightCntr < maxInFlight) {
cvsPipe.resume();
paused = false;
}
});
if (!paused && inFlightCntr >= maxInFlight) {
cvsPipe.pause();
paused = true;
}
}).on('end', () => {
console.log('CSV file successfully processed');
});
})();
<小时/>
如果您只是运行 csvPipe 将所有行收集到一个数组中(在处理任何行之前),代码可能会更简单一些。然后,您可以使用任意数量的 Promise 并发函数来处理数组,同时控制并行运行的数量。请参阅this answer从昨天开始,我们介绍了许多可让您在并行处理数组时管理并发性的函数。该实现的外观如下:
const csv = require('csv-parser');
const fs = require('fs');
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const maxInFlight = 10; // set this value to control how many pages run in parallel
const fname = 'ids.csv'
const csvPipe = fs.createReadStream(fname).pipe(csv());
const rowIDs = [];
async function getFile(rowId, path) {
try {
const page = await browser.newPage();
page.setViewport({ width: 1000, height: 1500, deviceScaleFactor: 1 });
let url = 'https://www.facebook.com/ads/library/?id=' + rowId;
const response = await page.goto(url, { waitUntil: 'networkidle2' });
await page.waitFor(3000);
const body = await page.$('body');
await body.screenshot({
path: path
});
} catch(e) {
console.log(e);
} finally {
await page.close();
}
}
csvPipe.on('data', row => {
rowIDs.push(row.ad_id);
}).on('end', () => {
// all rowIDs in the array now
pMap(rowIDs, (id) => {
let path = './images/' + id + '.png';
return getFile(id, path);
}, maxInFlight).then(() => {
console.log("all items processed"); // all done now
}).catch(err => {
console.log(e);
});
});
})();
// utility function for processing an array asynchronously with
// no more than limit items "in flight" at the same time
function pMap(array, fn, limit) {
return new Promise(function(resolve, reject) {
var index = 0, cnt = 0, stop = false, results = new Array(array.length);
function run() {
while (!stop && index < array.length && cnt < limit) {
(function(i) {
++cnt;
++index;
fn(array[i]).then(function(data) {
results[i] = data;
--cnt;
// see if we are done or should run more requests
if (cnt === 0 && index === array.length) {
resolve(results);
} else {
run();
}
}, function(err) {
// set stop flag so no more requests will be sent
stop = true;
--cnt;
reject(err);
});
})(index);
}
}
run();
});
}
关于javascript - puppeteer:迭代 CSV 文件和每行的屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59981135/
图像采集源除了显示控件(上一篇《.NET 控件转图片》有介绍从界面控件转图片),更多的是窗口以及屏幕。 窗口截图最常用的方法是GDI,直接上Demo吧: 1 private
我正在尝试编写一个程序来使用全局热键获取屏幕截图。下面是相应的代码: from datetime import datetime import os from pynput import keyboa
我正在构建一个应用程序,它应该为任何具有 Android 4 及更高版本的无根设备实现屏幕~镜像~,2 帧/秒就足够了。 我正在尝试使用 ADB“framebuffer:”命令来抓取设备屏幕截图 AD
如何使用 C++ 捕获屏幕截图?我将使用 Win32。 请不要使用 MFC 代码。 最佳答案 #include "windows.h" // should be less than and great
代码如下: import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; import java.aw
我目前正在构建一个 Google Chrome 扩展程序,该扩展程序可以从不同页面获取多个屏幕截图并将其发布到端点上。我遇到的问题是时间不对。我的意思是,在页面停止加载之前就太早截取屏幕截图了。其次,
我有一个 View Controller ,其中导航栏是透明的。我的下一个 View 是表格 View ,其中导航栏是白色的。 为了停止不需要的动画,我在表格 View 的“viewDidDissap
我正在尝试从多个 URL 制作屏幕截图。我的代码工作正常,但结果我得到了事件窗口的图像。但我需要带有浏览器顶部的完整屏幕截图(URL) file = open('links.txt', 'r', en
我正在尝试(并实现)获取屏幕截图: robot = new Robot(); BufferedImage biScreen = robot.createScreenCapture(rectScreen
是否有任何应用程序可以拍摄 android 设备的视频/屏幕截图。我知道在桌面上捕获屏幕视频/图像的软件很少,例如 camtasia、snagit。 Android 设备有类似的东西吗? 我知道使用
想要捕获可能处于非事件状态的选项卡的图像。 问题是,当使用此处显示的方法时,选项卡通常在捕获完成之前没有时间加载,从而导致失败。 chrome.tabs.update() 回调在标签页被捕获之前执行。
我想在新的 tkinter 窗口 (TopLevel) 中显示我的屏幕截图,但我不想将其保存在电脑上。当我保存它时它工作正常但是当我尝试从内存加载屏幕截图时出现错误:图像不存在。 我的主窗口是root
我正在 try catch 我当前所在的屏幕,因此当我覆盖下一个 View Controller 时,我可以使它成为它后面的 ImageView 并使其显示为半透明。这是有效的,但现在它在中间产生了一
我正在寻找将 docx(以及后来的 excel 和 powerpoint)文档的第一页转换为图像的方法。我宁愿不手动解析文档的整个 xml,因为这看起来工作量很大;) 所以我想我只是想收集一些关于如何
好吧,碰巧我正在编写一个程序来截取一些屏幕截图,并且在处理另一个进程已经在使用的文件时遇到了一些困难,希望有人能帮助我找到一种方法来“关闭”这个进程或启发我如何继续. //Create a new b
我即将在 App Store 上发布我的应用程序,我想截取我的应用程序的屏幕截图,但状态栏中没有所有信息,例如运营商和 Debug模式等。 我知道 Marshmallow 有一个 System UI
UIGraphicsBeginImageContext(self.reportList.frame.size); CGRect tableViewFrame = self.reportList.fra
是否有任何简洁的方法来访问 android 设备的屏幕截图以编程方式。我正在寻找大约 15-20fps。 我找到了一个代码android\generic\frameworks\base\service
好的,我正在尝试为多个网站运行多个屏幕截图!我已经获得了多个站点的一个屏幕截图,我还可以获得一个站点的多个 Viewport 屏幕截图,但我有 34 个站点需要这样做!那么有人知道用 casperjs
我正在为 iOS 制作一个贴纸包,在将其提交到 App Store 之前,我需要包含至少一张来自 5.5 英寸 iPhone 和 12.9 英寸 iPad Pro 的应用截图。这些都是我没有的设备。
我是一名优秀的程序员,十分优秀!