node.js - 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();
解决方案
在你的 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'
});
推荐阅读
- excel - 保存 minitab 图形和绘图时出错:“...集合为空且不包含有效的输出对象”
- laravel - 在 laravel 中使用中间件后 419 页面过期
- wpf - VisualStateManager 忽略 RepeatBehavior="Forever"
- swift - 我正在尝试删除存储在文档目录中的地图上的图钉,但出现此错误无法打开文件“SavedLocations”
- python - xpath 用 selenium 获得一个简单的 div id="xyz"
- javascript - 在选择单选按钮时,div 不在 html 中显示
- laravel - 如何传递图像“src”数据(带有文件夹地址)引导模式?
- android - 使用指定的 strings.xml
- javascript - 出现在视图中的元素在“开发工具中的元素”下找不到
- javascript - 从 JS 在浏览器中渲染表情符号