gpt4 book ai didi

javascript - Nightmare.js 屏幕截图缓冲区长度为 0

转载 作者:数据小太阳 更新时间:2023-10-29 05:21:26 24 4
gpt4 key购买 nike

我正在运行一个 nightmare.js 脚本,我试图在其中截取页面上多个元素的屏幕截图。

第一个 元素被捕获得很好,但折叠下方的所有其他元素都被捕获为零长度。我正在努力调试这个问题。任何帮助将不胜感激。

基本上,此脚本遍历页面并选择页面上所有 与选择器匹配的元素。然后,它使用 async 收集响应并返回一个对象缓冲区。问题是折叠下方的元素不会被截屏(缓冲区长度最终为零)。我尝试 wait() 并滚动到该元素,但到目前为止我还没有成功。

import * as Nightmare from 'nightmare'
import * as vo from 'vo'
import * as async from 'async'
import * as fs from 'fs'

const urls:String[] = [
'https://yahoo.com/'
]


Nightmare.action('snap', function(selector:String, done:Function) {
const self = this;

this.evaluate_now(function (selector) {
return Array.from(document.querySelectorAll(selector))
.map((ele:Element) => {
if (ele) {
const rect = ele.getBoundingClientRect()
const r:Function = Math.round
return {
x: r(rect.left),
y: r(rect.top),
width: r(rect.width),
height: r(rect.height)
}
}
})
}, function(err, clips) {
if (err) return done(err)
if (!clips) return done(new Error(`Selector not found`))
let snaps = []
const snap = (clip, cb) => {
self
.scrollTo(clip.y - clip.height, clip.x)
.screenshot(clip, cb)
.run()
}
async.mapSeries(clips.reverse(), snap, (err, res) => {
done(err, res)
})
}, selector)
})

const scrape = (url) => {
const nightmare = Nightmare({
show: true
});
nightmare
.goto(url)
.snap('.navbar')
.end()
.then((buffers:Buffer[]) => {
buffers.forEach((data, index) => {
fs.writeFileSync(`images/navbar-${index}.png`, data)
})
})
}

urls.forEach(scrape)

最佳答案

实际上,screenshot() 函数从可见屏幕获取坐标。
例如,如果任何元素的 (x,y) 是 (10, 1000) 并且您的窗口大小是 (800,600) 那么您可以滚动 (900:element.y, 0) 然后在 (element.y-scroll. y=100,元素.x)

我终于让代码工作了:

const Nightmare = require('nightmare');
const fs = require('fs');
const nightmare = Nightmare({
show: true,
openDevTools: true,
});

nightmare.goto('https://in.news.yahoo.com/')
.wait(1000)
.evaluate(getBounds, '.Cf')
.then(function(rects) {
console.log(rects);

function getScreenshot(rects, index) {
if (index == rects.length) return;
nightmare.scrollTo(rects[index].y, 0)
.screenshot(__dirname + '/images/navbar' + index + '.png', {
//60 is height of the top element which remains
x: rects[index].x-10,
y: 60,
width: rects[index].width+30,
height: rects[index].height +60
})
.then(function() {
console.log("Calling next. " + index);
getScreenshot(rects, index + 1);
}).catch(function(err) {
console.log(err);
})
};

getScreenshot(rects, 0);
})
.catch(function(err) {
console.log(err);
});

function getBounds(selector) {
var elements = document.querySelectorAll(selector);
if (elements && elements.length > 0) {
var arr = [];
const r = Math.round;
for (var ii = 0; ii < elements.length; ii++) {
var rect = elements[ii].getBoundingClientRect();
arr.push({
x: r(rect.left),
y: r(rect.top),
width: r(rect.width),
height: r(rect.height)
})
}
console.log("Elements found: ", arr.length);
return arr;
}
return null;
}

关于javascript - Nightmare.js 屏幕截图缓冲区长度为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44580348/

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