【问题标题】:Select the second table row of a table using puppeteer使用 puppeteer 选择表格的第二个表格行
【发布时间】:2019-09-16 05:27:24
【问题描述】:

我正在使用 node.js 和 puppeteer 在爬虫中工作,我的目标是获取表中两列的数据(日期和描述),代码工作正常,直到块从列中获取数据。 ..

下面的完整代码,包括我正在抓取的页面的网址:

const fs = require('fs');
const puppeteer = require('puppeteer');

const urlConsulta = "http://www.tre-pr.jus.br/";
const numeroProcessoSeq = "000000889";
const numeroProcessoAno = "2014";
const numeroProcessoDigito = "6160047";

var wait = ms => new Promise((r, j)=> setTimeout(r, ms));

void (async () => {
    try {
        const browser = await puppeteer.launch({
            headless: false
        });
        const page = await browser.newPage();
        await page.goto(urlConsulta);
        await page.select('#acao', 'pesquisarNumUnico');
        await page.evaluate((numeroProcessoSeq, numeroProcessoAno, numeroProcessoDigito) => {
            document.getElementById('numUnicoSequencial').value = numeroProcessoSeq;
            document.getElementById('numUnicoAno').value = numeroProcessoAno;
            document.getElementById('numUnicoOrigem').value = numeroProcessoDigito;
        }, numeroProcessoSeq, numeroProcessoAno, numeroProcessoDigito);

        await page.$eval('form[action*="http://www.tre-pr.jus.br/@@processrequest"]', form => form.submit());

        await page.waitForNavigation();
        var frame = await page.frames().find(f => f.name() === 'ifr_servicos');
        await frame.click('a[href*="ExibirDadosProcesso"]');
        await page.frames().find(f => f.name() === 'ifr_servicos');
        await wait(10000);
        await frame.click('[name*="todos"]');
        await frame.$eval('[name*="ExibirPartesProcessoZona"]', form => form.submit());
        await wait(10000);
        let string = await buscaFases(frame);
        fs.writeFile("teste.txt", string, function(err) {
            if(err) {
                return console.log(err);
            }
            console.log("The file was saved!");
        }); 
        console.log(string);
        await wait(10000);
        await browser.close();
    } catch (error) {
        console.log(error);
    }
})();

async function buscaFases(frame) {
    return await frame.evaluate(() => {
        let div = document.querySelector('div[id*="conteudo"]');
        let rowns = Array.from(div.children[4].children[0].children);
        let movimentosInfo = rowns.map(row => {
          let data = row.querySelector("tr td:first-child").textContent;
          let descricao = row.querySelector("tr td:first-child + td").textContent;
          return { data, descricao };
        });
        return JSON.stringify(movimentosInfo);
    });
};

获取数据的具体行:

let data = row.querySelector("tr td:first-child").textContent;
let descricao = row.querySelector("tr td:first-child + td").textContent;

【问题讨论】:

    标签: javascript html node.js puppeteer


    【解决方案1】:

    问题在于并非所有tr 都具有您所期望的子元素。这可能是因为带有 colspan 的 td 标记。所以你应该首先过滤你的数组以将其他元素排序出来。

    代码

    let movimentosInfo = ... 开始更改您的行,包括您的地图功能:

    let movimentosInfo = rowns.filter(row => {
        return row.querySelector("tr td:first-child") && row.querySelector("tr td:first-child + td");
    }).map(row => {
        let data = row.querySelector("tr td:first-child").textContent;
        let descricao = row.querySelector("tr td:first-child + td").textContent;
        return { data, descricao };
    });
    

    这增加了一个过滤功能,在映射其内容之前测试所需元素是否存在。

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 2011-12-28
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 2012-03-03
      相关资源
      最近更新 更多