首页 > 解决方案 > Nodejs/Puppeteer - 导航超时

问题描述

我需要帮助来理解超时是如何工作的,尤其是对于节点/木偶

我阅读了有关此的所有堆栈问题和 github 问题,但我可以找出问题所在

可能是我的代码...

当我运行这个文件时,我收到来自图像的错误。你可以看到我尝试修复它的方法,没有任何效果

有人可以解释为什么会发生这种情况以及避免这种情况的最佳方法吗?有没有更好的方法来获得这些项目?

//vou até os seeds em x tempo
var https = require('https');
var Q = require('q');
var fs = require('fs');
var puppeteer = require('puppeteer');
var Projeto = require('./Projeto.js');

const url = 'https://www.99freelas.com.br/projects?categoria=web-e-desenvolvimento'
/*const idToScrape;
deverá receber qual a url e os parametros específicos de cada seed */
async function genScraper() {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    //page.setDefaultNavigationTimeout(60000);
    page.waitForNavigation( { timeout: 60000, waitUntil: 'domcontentloaded' });

    await page.goto(url);


    var projetos = await page.evaluate(() => {
        let qtProjs = document.querySelectorAll('.result-list li').length;

        let listaDeProjs = Array.from(document.querySelectorAll('.result-list li'));

        let tempProjetos = [];

        for( var i=0; i<=listaDeProjs.length; i++ ) {
            let titulo = listaDeProjs[i].children[1].children[0].textContent;
            let descricao = listaDeProjs[i].children[2].textContent;
            let habilidades = listaDeProjs[i].children[3].textContent;
            let publicado = listaDeProjs[i].children[1].children[1].children[0].textContent;
            let tempoRestante = listaDeProjs[i].children[1].children[1].children[1].textContent;
            //let infoCliente;
            proj = new Projeto(titulo, descricao, habilidades, publicado, tempoRestante);
            tempProjetos.push(proj);
        }

        return tempProjetos;
    });

    console.log(projetos);
    browser.close();
}

genScraper();

在此处输入图像描述

标签: node.jspuppeteer

解决方案


在你的 for 循环中,

for( var i=0; i<=listaDeProjs; i++ ) {
   ...
}

listaDeProjs应该listaDeProjs.length

如果此路径上的任何地方未定义,您的评估脚本将在多个地方失败:(例如,如果children[1]未定义或children[0]未定义。)

listaDeProjs[i].children[1].children[0].textContent;

您可以执行以下操作lodash

_.get(listaDeProjs[i],"children[1].children[0].textContent","")

""如果没有这样的值,那将默认为。

此外,以下内容通过https://try-puppeteer.appspot.com/与您在 1.7 中的代码完美配合

await page.goto(url, {
    waitUntil: 'networkidle2',
    timeout: '5000'
});

推荐阅读