这是我的第一篇文章。我用Nodejs学习JS。所以我看到了Puppeter的脚本来截图元素,我复制并更改了它。但这里有一些问题。
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Veamos Kia!'))
app.listen(port, () => console.log(`App de ejemplo escuchando el puerto ${port}!`))
app.get("/scrapping", function (req, res) {
res.send('Estamos listos para empezar');
})
const puppeteer = require("puppeteer");
const mkdirp = require('mkdirp')
//Vemos y formateamos la fecha en la que las capturas se hacen
let date = new Date()
let day = date.getDate()
let month = date.getMonth() + 1
let year = date.getFullYear()
if(month < 10){
console.log(`${day}-0${month}-${year}`)
}else{
console.log(`${day}-${month}-${year}`)
}
//fecha formateada a DD/MM/AAAA
let dateFormat = `${day}${month}${year}`;
//fecha formateada todo seguido
let dateNumber = Number(date);
const fs = require("fs");
//ES LA BASE
//const puppeteer = require('puppeteer'); // include lib
(async () => { // declare function
const browser = await puppeteer.launch({'defaultViewport' : { 'width' : 1920, 'height' : 545 }}); // run browser
const page = await browser.newPage(); // open new tab
const navigationPromise = page.waitForNavigation()
await page.goto('https://www.fiat.cl/modelo/ducato-pasajeros/ducato-pasajeros-precios-y-versiones/#top');
await page.waitForSelector('div.small-12.large-12.columns.container'); // wait for the selector to load
let element = await page.$('div.small-12.large-12.columns.container'); // declare a variable with an ElementHandle
await element.screenshot({path: `fiat/${dateFormat}/1.png`}); // take screenshot element in puppeteer
await page.goto('https://www.fiat.cl/modelo/argo/argo-precios-y-versiones/#top');
await navigationPromise
await page.waitForSelector('div.small-12.large-12.columns.container'); // wait for the selector to load
await element.screenshot({path: `fiat/${dateFormat}/2.png`}); // take screenshot element in puppeteer
await browser.close(); // close browser
})();
木头扔给我这个;"(节点:17856(未处理的PromiseRejection警告:错误:节点不可见或不是HTMLElem";
第一张截图还可以,但第二张有错误。我做错了什么?很抱歉我犯了语法错误,我不会说英语,但我正在努力一天比一天进步。
第二个屏幕截图很可能不起作用,因为您有一个旧element
的句柄,该句柄已无法访问(您已导航到其他页面(。您必须重新初始化element
变量:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Veamos Kia!'))
app.listen(port, () => console.log(`App de ejemplo escuchando el puerto ${port}!`))
app.get("/scrapping", function (req, res) {
res.send('Estamos listos para empezar');
})
const puppeteer = require("puppeteer");
const mkdirp = require('mkdirp')
//Vemos y formateamos la fecha en la que las capturas se hacen
let date = new Date()
let day = date.getDate()
let month = date.getMonth() + 1
let year = date.getFullYear()
if(month < 10){
console.log(`${day}-0${month}-${year}`)
}else{
console.log(`${day}-${month}-${year}`)
}
//fecha formateada a DD/MM/AAAA
let dateFormat = `${day}${month}${year}`;
//fecha formateada todo seguido
let dateNumber = Number(date);
const fs = require("fs");
//ES LA BASE
//const puppeteer = require('puppeteer'); // include lib
(async () => { // declare function
const browser = await puppeteer.launch({'defaultViewport' : { 'width' : 1920, 'height' : 545 }}); // run browser
const page = await browser.newPage(); // open new tab
const navigationPromise = page.waitForNavigation()
await page.goto('https://www.fiat.cl/modelo/ducato-pasajeros/ducato-pasajeros-precios-y-versiones/#top');
await page.waitForSelector('div.small-12.large-12.columns.container'); // wait for the selector to load
let element = await page.$('div.small-12.large-12.columns.container'); // declare a variable with an ElementHandle
await element.screenshot({path: `1.png`}); // take screenshot element in puppeteer
await page.goto('https://www.fiat.cl/modelo/argo/argo-precios-y-versiones/#top');
await navigationPromise
await page.waitForSelector('div.small-12.large-12.columns.container'); // wait for the selector to load
let elementAfterNavigation = await page.$('div.small-12.large-12.columns.container'); // re-initialize element!
await elementAfterNavigation.screenshot({path: `2.png`}); // take screenshot element in puppeteer
await browser.close(); // close browser
})();
注意elementAfterNavigation
。这就是为什么这样的功能(例如,单个屏幕截图(值得拥有自己的具有作用域变量的功能。